vue中字典的使用
vue中字典的使用
1.引入字典
dicts: ['order_status','product_type'],
2.表單中使用
select下拉
<el-form-item label="訂單狀態(tài)" prop="orderStatus"> <el-select v-model="form.orderStatus" clearable placeholder="請(qǐng)輸入訂單狀態(tài)" :disabled="isDisable" style="width: 100%" > <el-option v-for="dict in dict.type.order_status" :key="dict.value" :label="dict.label" :value="dict.value" /> </el-select> </el-form-item>
checkbox
<el-form-item label="是否正式報(bào)價(jià)單" prop="isFormal"> <el-radio-group v-model="form.isFormal"> <el-radio v-for="dict in dict.type.yes_or_no" :key="dict.value" :label="dict.value">{{dict.label}}</el-radio> </el-radio-group> </el-form-item>
3.列表中使用
<el-table-column label="訂單狀態(tài)" align="center" prop="orderStatus" > <template slot-scope="scope"> <dict-tag :options="dict.type.order_status" :value="scope.row.orderStatus"/> </template> </el-table-column>
vue項(xiàng)目中字典如何使用(其中一種解決方案)
整體思想:
1、新建一個(gè)文件(此處是dict.js),一般是在mixin里面,將所有的字典項(xiàng)以數(shù)組的形式聲明好
2、在create中判斷字典值是否存在于state中,若不存在,則全量引入(保存在store中)
具體實(shí)施:
1、在dict.js中判斷到字典項(xiàng)不存在時(shí),將所有的字典項(xiàng)逗號(hào)分隔形成一個(gè)字符串(codes)傳給store的action
1.1、在store的字典項(xiàng)模塊中定義一個(gè)action,該action方法調(diào)用接口獲取所有字典值,將字典項(xiàng)設(shè)置入state中
1.2、(接口的定義:傳入多個(gè)字典項(xiàng)的值,逗號(hào)分隔,返回?cái)?shù)據(jù)是各個(gè)字典項(xiàng)的值)
1.3、遍歷返回的值,調(diào)用mutation方法,將多個(gè)字典值以對(duì)象的形式存入state中,屬性名是字典項(xiàng)的值,屬性值是一個(gè)數(shù)組,數(shù)組里面是該字典項(xiàng)的所有字典值對(duì)象
2、在需要用到字典的地方,引入mapGetters,調(diào)用對(duì)應(yīng)GET方法即可使用
// 文件路徑 src/mixins/dict.jsimport { mapGetters, mapActions } from 'vuex'; export default { data() { return { // 所有的字典項(xiàng) dictList: [ 'comm.yesOrNo', 'comm.hasOrNot', 'comm.gender', ] }; }, computed: { ...mapGetters('dict', ['GET_VIEWS_DICLIST']) }, async created() { if (this.GET_VIEWS_DICLIST['comm.yesOrNo'] === undefined) { this.SET_VIEWS_DICLIST_ACTIONS({ codes: this.dictList.join(',') }); } }, methods: { ...mapActions('dict', ['SET_VIEWS_DICLIST_ACTIONS']), // 該方法根據(jù)字典值獲取字典名稱 // val 字典值,例如: 1 // list 該字典類型的所有字典項(xiàng) getLabelName(val, list) { const len = list?.length; for (let i = 0; i < len; i++) { if (list[i].code === String(val)) { return list[i].name; } } return ''; }, } };
字典相關(guān)的狀態(tài)管理模塊
// 文件位置 src/store/modules/dict.js// 接口引入import { getViewsDict } from '@/api/base/common'; const getDefaultState = () => { return { dicList: {} // 業(yè)務(wù)字典 }; }; const dict= { namespaced: true, state: getDefaultState(), getters: { GET_VIEWS_DICLIST: (state) => state.dicList }, actions: { SET_VIEWS_DICLIST_ACTIONS({ commit }, codes) { return new Promise((resolve, reject) => { getViewsDict(codes) .then((res) => { const obj = {}; res?.data?.forEach((item) => { obj[item.code] = item?.items?.map((val) => { return { code: String(val.code), name: val.name }; }); }); commit('SET_VIEWS_DICLIST', obj); resolve(obj); }) .catch(() => { reject(); }); }); } }, mutations: { SET_VIEWS_DICLIST: (state, data) => { state.dicList = Object.assign({}, state.dicList, data); } } }; export default dict;
獲取字典值的接口
// 文件位置 src/api/base/common.jsimport request from '@/api/axios.interceptors'; const viewApi = process.env.VUE_APP_VIEWS_API; // 獲取業(yè)務(wù)字典 export function getViewsDict(params) { return request({ url: `${viewApi}/dictionary`, method: 'get', params }); }
字典值的使用,使用之前記得引入src/mixins/dict.js內(nèi)容
<strong><el-form-item class="form-item" label="戶籍類型" prop="residentType"> <el-select v-model="formData_base.residentType" placeholder="請(qǐng)選擇" @change="changeResidentType" > <el-option v-for="item in GET_VIEWS_DICLIST['comm.registerType.resident']" :key="item.code" :label="item.name" :value="item.code" ></el-option> </el-select> </el-form-item></strong>
到此這篇關(guān)于vue中字典的使用的文章就介紹到這了,更多相關(guān)vue字典使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue.js中extend選項(xiàng)和delimiters選項(xiàng)的比較
這篇文章主要介紹了Vue.js中extend選項(xiàng)和delimiters選項(xiàng)的比較的相關(guān)資料,需要的朋友可以參考下2017-07-07vue中報(bào)錯(cuò)Duplicate?keys?detected:'1'.?This?may?c
我們?cè)趘ue開發(fā)過程中常會(huì)遇到一些錯(cuò)誤,這篇文章主要給大家介紹了關(guān)于vue中報(bào)錯(cuò)Duplicate?keys?detected:‘1‘.?This?may?cause?an?update?error的解決方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03vue element upload組件 file-list的動(dòng)態(tài)綁定實(shí)現(xiàn)
這篇文章主要介紹了vue element upload組件 file-list的動(dòng)態(tài)綁定實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10vue.js編譯時(shí)給生成的文件增加版本號(hào)
這篇文章主要介紹了vue.js編譯時(shí)給生成的文件增加版本號(hào),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09vue-cli3 取消eslint校驗(yàn)代碼的解決辦法
這篇文章主要介紹了vue-cli3 取消eslint校驗(yàn)代碼的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01Vue3項(xiàng)目中預(yù)覽并打印PDF的兩種方法
最近在項(xiàng)目開發(fā)中碰到一個(gè)需求是在頁面中展示pdf預(yù)覽功能,下面這篇文章主要給大家介紹了關(guān)于Vue3項(xiàng)目中預(yù)覽并打印PDF的兩種方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05