Commit e69750a5 by moxun

简化数据字典,支持单组件内多字典同时使用,全局混入,组件不配置字典时,不对data附加dict属性

parent ae71ad97
import Vue from 'vue'
import { get as getDictDetail } from '@/api/dictDetail'
export default class Dict {
constructor (dict) {
this.dict = dict
}
async init (names, completeCallback) {
if (names === undefined || name === null) {
throw new Error('need dict names')
}
const ps = []
names.forEach(n => {
Vue.set(this.dict.dict, n, {})
Vue.set(this.dict.label, n, {})
Vue.set(this.dict, n, [])
ps.push(getDictDetail(n).then(data => {
this.dict[n].splice(0, 0, ...data.content)
data.content.forEach(d => {
Vue.set(this.dict.dict[n], d.value, d)
Vue.set(this.dict.label[n], d.value, d.label)
})
}))
})
await Promise.all(ps)
completeCallback()
}
}
import Dict from './Dict'
const install = function (Vue) {
Vue.mixin({
data () {
if (this.$options.dicts instanceof Array) {
const dict = {
dict: {},
label: {},
}
return {
dict,
}
}
return {}
},
created () {
if (this.$options.dicts instanceof Array) {
new Dict(this.dict).init(this.$options.dicts, () => {
this.$nextTick(() => {
this.$emit('dictReady')
})
})
}
},
})
}
export default { install }
...@@ -11,6 +11,7 @@ import '@/styles/index.scss' // global css ...@@ -11,6 +11,7 @@ import '@/styles/index.scss' // global css
import App from './App' import App from './App'
import router from './router/routers' import router from './router/routers'
import permission from './components/permission' import permission from './components/permission'
import dict from './components/dict'
import store from './store' import store from './store'
import '@/icons' // icon import '@/icons' // icon
...@@ -19,6 +20,7 @@ import './router/index' // permission control ...@@ -19,6 +20,7 @@ import './router/index' // permission control
Vue.use(mavonEditor) Vue.use(mavonEditor)
Vue.use(permission) Vue.use(permission)
Vue.use(dict)
Vue.use(ElementUI, { locale }) Vue.use(ElementUI, { locale })
Vue.config.productionTip = false Vue.config.productionTip = false
......
<template> <template>
<div class="app-container"> <div class="app-container">
<!--form 组件--> <!--form 组件-->
<eForm ref="form" :is-add="isAdd" :dicts="dicts"/> <eForm ref="form" :is-add="isAdd" :dicts="dict.user_status"/>
<el-row :gutter="20"> <el-row :gutter="20">
<!--部门数据--> <!--部门数据-->
<el-col :xs="9" :sm="6" :md="4" :lg="4" :xl="4"> <el-col :xs="9" :sm="6" :md="4" :lg="4" :xl="4">
...@@ -53,9 +53,7 @@ ...@@ -53,9 +53,7 @@
</el-table-column> </el-table-column>
<el-table-column label="状态" align="center"> <el-table-column label="状态" align="center">
<template slot-scope="scope"> <template slot-scope="scope">
<div v-for="item in dicts" :key="item.id"> <el-tag>{{ dict.label.user_status[scope.row.enabled] }}</el-tag>
<el-tag v-if="scope.row.enabled.toString() === item.value" :type="scope.row.enabled ? '' : 'info'">{{ item.label }}</el-tag>
</div>
</template> </template>
</el-table-column> </el-table-column>
<el-table-column :show-overflow-tooltip="true" prop="createTime" label="创建日期"> <el-table-column :show-overflow-tooltip="true" prop="createTime" label="创建日期">
...@@ -97,7 +95,6 @@ ...@@ -97,7 +95,6 @@
<script> <script>
import checkPermission from '@/utils/permission' import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData' import initData from '@/mixins/initData'
import initDict from '@/mixins/initDict'
import { del, downloadUser } from '@/api/user' import { del, downloadUser } from '@/api/user'
import { getDepts } from '@/api/dept' import { getDepts } from '@/api/dept'
import { parseTime, downloadFile } from '@/utils/index' import { parseTime, downloadFile } from '@/utils/index'
...@@ -105,7 +102,9 @@ import eForm from './form' ...@@ -105,7 +102,9 @@ import eForm from './form'
export default { export default {
name: 'User', name: 'User',
components: { eForm }, components: { eForm },
mixins: [initData, initDict], mixins: [initData],
// 设置数据字典
dicts: ['user_status'],
data() { data() {
return { return {
height: document.documentElement.clientHeight - 180 + 'px;', isAdd: false, height: document.documentElement.clientHeight - 180 + 'px;', isAdd: false,
...@@ -125,8 +124,6 @@ export default { ...@@ -125,8 +124,6 @@ export default {
this.getDeptDatas() this.getDeptDatas()
this.$nextTick(() => { this.$nextTick(() => {
this.init() this.init()
// 加载数据字典
this.getDict('user_status')
}) })
}, },
mounted: function() { mounted: function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment