前端开发异步请求数据字典轻松调用

第一步:创建公共方法

首先创建公共调用数据字典方法
路径:src\utils\dictionary.js(个人习惯)
注意:后面会做更好的完善,现在先用着

// dictionary.js
/**
 * 字典
 * @param {*} that Vue对象
 */
export function dictionaries(that, key, code) {
  let param = {
    dictCode: code
  };
  // 实际使用
  that.api_ifms.getDictList(param).then(res => {
    if (res.code === 200) {
      that[key] = res.data;
    }
  });
}

第二步:注册到vue全局使用

第二步就是在main.js里开始注册vue的自定义全局方法
路径:src\main.js

import { dictionaries } from "@/utils/dictionary.js";
Vue.prototype.dictionaries = dictionaries;

第三步:组件里调用

<template>
  <div>
	{{allotTypes}}
  </div>
</template>

<script>

export default {
  data() {
    return {
      allotTypes: [],
    };
  },
  created() {
    let that = this;
    // 字典
    this.dictionaries(that, 'allotTypes', 'WITHDRAWAL_STATUS') 
  },
  methods: {
	// 无须在这里创建请求字典数据的方法
    },
  }
};
</script>
<style lang="scss" scoped>
</style>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值