# 为什么使用uni-config-center
实际开发中很多插件需要配置文件才可以正常运行,如果每个插件都单独进行配置的话就会产生下面这样的目录结构
```bash
cloudfunctions
└─────common 公共模块
├─plugin-a // 插件A对应的目录
│ ├─index.js
│ ├─config.json // plugin-a对应的配置文件
│ └─other-file.cert // plugin-a依赖的其他文件
└─plugin-b // plugin-b对应的目录
├─index.js
└─config.json // plugin-b对应的配置文件
```
假设插件作者要发布一个项目模板,里面使用了很多需要配置的插件,无论是作者发布还是用户使用都是一个大麻烦。
uni-config-center就是用了统一管理这些配置文件的,使用uni-config-center后的目录结构如下
```bash
cloudfunctions
└─────common 公共模块
├─plugin-a // 插件A对应的目录
│ └─index.js
├─plugin-b // plugin-b对应的目录
│ └─index.js
└─uni-config-center
├─index.js // config-center入口文件
├─plugin-a
│ ├─config.json // plugin-a对应的配置文件
│ └─other-file.cert // plugin-a依赖的其他文件
└─plugin-b
└─config.json // plugin-b对应的配置文件
```
使用uni-config-center后的优势
- 配置文件统一管理,分离插件主体和配置信息,更新插件更方便
- 支持对config.json设置schema,插件使用者在HBuilderX内编写config.json文件时会有更好的提示(后续HBuilderX会提供支持)
# 用法
在要使用uni-config-center的公共模块或云函数内引入uni-config-center依赖,请参考:[使用公共模块](https://uniapp.dcloud.net.cn/uniCloud/cf-common)
```js
const createConfig = require('uni-config-center')
const uniIdConfig = createConfig({
pluginId: 'uni-id', // 插件id
defaultConfig: { // 默认配置
tokenExpiresIn: 7200,
tokenExpiresThreshold: 600,
},
customMerge: function(defaultConfig, userConfig) { // 自定义默认配置和用户配置的合并规则,不设置的情况侠会对默认配置和用户配置进行深度合并
// defaudltConfig 默认配置
// userConfig 用户配置
return Object.assign(defaultConfig, userConfig)
}
})
// 以如下配置为例
// {
// "tokenExpiresIn": 7200,
// "passwordErrorLimit": 6,
// "bindTokenToDevice": false,
// "passwordErrorRetryTime": 3600,
// "app-plus": {
// "tokenExpiresIn": 2592000
// },
// "service": {
// "sms": {
// "codeExpiresIn": 300
// }
// }
// }
// 获取配置
uniIdConfig.config() // 获取全部配置,注意:uni-config-center内不存在对应插件目录时会返回空对象
uniIdConfig.config('tokenExpiresIn') // 指定键值获取配置,返回:7200
uniIdConfig.config('service.sms.codeExpiresIn') // 指定键值获取配置,返回:300
uniIdConfig.config('tokenExpiresThreshold', 600) // 指定键值获取配置,如果不存在则取传入的默认值,返回:600
// 获取文件绝对路径
uniIdConfig.resolve('custom-token.js') // 获取uni-config-center/uni-id/custom-token.js文件的路径
// 引用文件(require)
uniIDConfig.requireFile('custom-token.js') // 使用require方式引用uni-config-center/uni-id/custom-token.js文件。文件不存在时返回undefined,文件内有其他错误导致require失败时会抛出错误。
// 判断是否包含某文件
uniIDConfig.hasFile('custom-token.js') // 配置目录是否包含某文件,true: 文件存在,false: 文件不存在
```
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
原创微信小程序工具类带支付投票等功能 (323个子文件)
mdi_base64.css 922KB
fontawesome_base64.css 228KB
mian.min.css 206KB
mianNvue.min.css 205KB
iconfont.css 72KB
light-green.css 22KB
deep-purple.css 22KB
deep-orange.css 21KB
light-blue.css 21KB
blue-grey.css 21KB
primary.css 21KB
purple.css 21KB
yellow.css 21KB
indigo.css 21KB
orange.css 21KB
amber.css 20KB
brown.css 20KB
green.css 20KB
lime.css 20KB
blue.css 20KB
cyan.css 20KB
teal.css 20KB
pink.css 20KB
red.css 20KB
demo.css 8KB
theme.css 1015B
index.html 180KB
index.html 2KB
b1.jpg 22KB
b2.jpg 20KB
phaser.min.js 1018KB
echarts.min.js 549KB
lottie.js 268KB
index.js 139KB
index.js 139KB
index.js 139KB
index.js 139KB
index.js 139KB
index.js 139KB
iconfont.js 123KB
area.js 122KB
crender.min.js 96KB
crender.class.js 54KB
qrcode.js 38KB
graphs.js 31KB
weapp-qrcode.js 31KB
findCnChart.js 29KB
qrcode_wx.js 26KB
calendar.js 18KB
path2d.js 17KB
city.js 14KB
signBoard.js 14KB
style.class.js 13KB
path2d.js 12KB
md5.js 12KB
index.js 11KB
index.js 11KB
index.js 11KB
clipboardJS.js 11KB
bezierCurveToPolyline.js 10KB
graph.class.js 10KB
util.js 9KB
upload.js 9KB
index.js 9KB
js-search.min.js 8KB
index.js 7KB
dayjs.min.js 6KB
index.js 6KB
color.js 5KB
util.js 5KB
curves.js 4KB
test.js 4KB
keywords.js 4KB
mixin.js 4KB
polylineToBezierCurve.js 4KB
index.js 3KB
index.js 3KB
index.js 3KB
index.js 3KB
index.js 3KB
index.js 3KB
index.js 3KB
index.js 3KB
ring.js 3KB
index.js 3KB
index.js 3KB
tm-vuetify.js 2KB
index.js 2KB
common.js 2KB
emitter.js 2KB
uni-echarts-canvas.js 2KB
getParent.js 2KB
guid.js 2KB
zh-cn.min.js 2KB
index.js 2KB
relativeTime.min.js 2KB
requestAnimationFrame.js 1KB
canvas.js 1KB
mixin.js 1KB
province.js 1KB
共 323 条
- 1
- 2
- 3
- 4
资源评论
- TiMor灬Captain2022-10-02怎么使用?
浮生若梦l
- 粉丝: 126
- 资源: 16
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 软件仿真多机串行通信.doc
- Python大作业:音乐播放软件(爬虫+可视化+数据分析+数据库)
- 课程设计-python爬虫-爬取日报,爬取日报文章后存储到本地,附带源代码+课程设计报告
- 软件和信息技术服务行业投资与前景预测.pptx
- 课程设计-基于SpringBoot + Mybatis+python爬虫NBA球员数据爬取可视化+源代码+文档+sql+效果图
- 软件品质管理系列二项目策划规范.doc
- 基于TensorFlow+PyQt+GUI的酒店评论情感分析,支持分析本地数据文件和网络爬取数据分析+源代码+文档说明+安装教程
- 软件定义无线电中的模拟电路测试技术.pptx
- 软件开发协议(作为技术开发合同附件).doc
- 软件开发和咨询行业技术趋势分析.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功