Files
faw-report-library-web/src/components/dict.js
T

59 lines
1.2 KiB
JavaScript

// 混入字典方法 做到按需加载
import interfaces from "@/api/modules/dictionary";
import store from "@/store";
import Vue from "vue";
class Dict {
constructor(dict) {
this.dict = dict;
}
async init(names) {
const ps = [];
names.forEach((name) => {
if (store.getters.dicts[name]) {
Vue.set(this.dict, name, store.getters.dicts[name]);
} else {
Vue.set(this.dict, name, []);
ps.push(
interfaces.getCode(name).then((res) => {
this.dict[name] = Object.freeze(res.data);
store.dispatch('setDicts_a', {
label: name,
value: Object.freeze(res.data),
})
})
);
}
});
await Promise.all(ps);
}
}
const install = function (Vue) {
Vue.mixin({
data() {
if (
this.$options.dicts instanceof Array &&
this.$options.dicts.length > 0
) {
return { dict: {} };
} else {
return {};
}
},
created() {
if (
this.$options.dicts instanceof Array &&
this.$options.dicts.length > 0
) {
new Dict(this.dict).init(this.$options.dicts);
}
},
});
};
export default { install };