import Dict from './Dict'
|
import { mergeOptions } from './DictOptions'
|
|
/**
|
* 业务字典插件(Vue3 版)。
|
* 页面通过组件选项 `dicts: ['xxx']` 声明所需字典,插件用全局 mixin 注入 `this.dict`,
|
* 供模板以 `dict.type.xxx` / `dict.label.xxx` 消费。
|
*/
|
export default function(app, options) {
|
mergeOptions(options)
|
app.mixin({
|
data() {
|
if (this.$options === undefined || this.$options.dicts === undefined || this.$options.dicts === null) {
|
return {}
|
}
|
const dict = new Dict()
|
dict.owner = this
|
return {
|
dict
|
}
|
},
|
created() {
|
if (!(this.dict instanceof Dict)) {
|
return
|
}
|
options.onCreated && options.onCreated(this.dict)
|
this.dict.init(this.$options.dicts).then(() => {
|
options.onReady && options.onReady(this.dict)
|
this.$nextTick(() => {
|
this.$emit('dictReady', this.dict)
|
if (this.$options.methods && this.$options.methods.onDictReady instanceof Function) {
|
this.$options.methods.onDictReady.call(this, this.dict)
|
}
|
})
|
})
|
},
|
})
|
}
|