vue3 封装全局公共computed方法

本文介绍了如何在Vue项目中创建全局computed方法,如formatAirPort,用于处理Vuex状态中的机场数据。在main.js中安装全局组件,并给出使用示例。
摘要由CSDN通过智能技术生成

1,创建一个名为globalComputed.js的文件

// 全局computed方法
/**
 * 使用formatAirPort(params)
 * 
 * 
 */
const Global = {
    install(app) {
        // 起始站
        app.config.globalProperties.formatAirPort = function () {
            return function (id) {
                if (!id) {
                    return "";
                } else {
                    return this.$store.state.airportSelect.options.filter((el) => el.airportCode == id)[0]?.airportApsn;
                }
            };
        }()
        // 航班类型
        app.config.globalProperties.formatWayBillType = function () {
            return function (id) {
                if (!id) {
                    return "";
                } else {
                    return this.$store.state.wayBillType.options.filter((el) => el.code == id)[0]?.name;
                }
            };
        }()
        // 航班性质
        app.config.globalProperties.formatFlightNature = function () {
            return function (id) {
                if (!id) {
                    return "";
                } else {
                    return this.$store.state.flightNature.options.filter((el) => el.code == id)[0]?.name;
                }
            };
        }()
    },
};

export default Global;

2,在主入口(main.js)安装该全局组件

import globalComputed from '@/utils/globalComputed'
app.use(globalComputed);

3,使用示例

vuex数据:[{“airportId”:“1”,“airportCode”:“XIY”,“airportIcao”:“”,“airportApsn”:“西安”,“cnName”:“西安咸阳国际机场”,“enName”:“”,“cityCode”:“”,“remark”:“”,“countryCode”:“”},{“airportId”:“2”,“airportCode”:“XNN”,“airportIcao”:“”,“airportApsn”:“西宁”,“cnName”:“西宁曹家堡机场”,“enName”:“”,“cityCode”:“”,“remark”:“”,“countryCode”:“”}],“datetime”:1708401151145}]

formatAirPort(params)//西安
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值