在utils文件夹下面新建一个common.js文件
import lodash from "lodash";
let base = {
/**
* 根据字典吸key获取value
* @param {Array} list [{key: "", value: ""}]
* @param {string} key
* @returns {string} value
*/
getDictionaryValue(list, key) {
let arr = list.filter(item => item.key == key)
let val = ''
if (arr.length > 0) {
val = lodash.get(arr, '[0]').value //这里用到了一个插件lodash
} else {
val = ''
}
return val
}
}
export {
base
}
在main.js里引用并挂载
import App from './App'
import { http } from '@/request/service.js'
import { base } from './utils/common' //引入base
// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
Vue.prototype.$http = http
Vue.prototype.base = base //挂载
App.mpType = 'app'
const app = new Vue({
...App
})
app.$mount()
// #endif
// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
const app = createSSRApp(App)
app.config.globalProperties.$http = http
app.config.globalProperties.base = base
return {
app
}
}
// #endif
在request文件夹下新建common.js文件用来定义接口
import { http } from '@/request/service.js'
/**
* 获取字典
* @param {Array} keys - 查询参数
*/
const getVueDictionary = (keys) => {
return http.post('/api/Sys_Dictionary/GetVueDictionary', keys)
}
export {
getVueDictionary
}
在具体的文件中使用(base调用)
<view class="item-card" v-for="(item,index) in dataList" :key="index">
<view class="info">
<view class="item">
<view class="item-label">险种类型:</view>
<view class="">{{ base.getDictionaryValue(InsuTypeList,item.InsuType) }}</view>
</view>
<view class="item">
<view class="item-label">就诊凭证类型:</view>
<view class="">{{ base.getDictionaryValue(MdtrtCertTypeList,item.CertType) }}</view>
</view>
</view>
</view>
import { getVueDictionary } from "@/request/common.js" //引入定义好的接口
export default {
data() {
return {
dataList: [],
InsuTypeList: [], //险种类型
MdtrtCertTypeList: [], // 就诊凭证类型
}
},
created() {
this.getVueDictionary()
},
methods: {
getVueDictionary(['InsuType', 'MdtrtCertType']).then(res => {
res.data.forEach(item => {
this[`${item.dicNo}List`] = item.data
})
})
}
}