在全局定义一个字典查询的公共方法

在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
			})
		})
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值