typescript+vue如何将多个常量类挂在到vue原型的$const属性上?

以下是我目前的实现方式,无法解决这个问题

我想以 this.$const.color.red 获取CommonConst 类中的red常量和以

this.$const.routerName.login的方式获取RouterName 类中的login;

但因为typescript是单继承,所以我只能让Constants 类继承一个CommonConst ,不能将CommonConst和RouterName 一起继承

同时希望以面向对象的方式实现,编辑器可以通过类型提示出来 我输 this.$const.routerName.的时候就能提示出来routerName类中的所有属性

export default class RouterName {
    public static login: string = 'login'
    public static authentication: string = 'authentication'
    public static search: string = 'search'
    public static home: string = 'home'
    public static talkList: string = 'talkList'
    public static voice: string = 'voice'
    public static voiceList: string = 'voiceList'
    public static chat: string = 'chat'
    public static message: string = 'message'
    public static userDetail: string = 'userDetail'
    public static match: string = 'match'
    public static talkDetail: string = 'talkDetail'
    public static notify: string = 'notify'
    public static mine: string = 'mine'
    public static talkAdd: string = 'talkAdd'
}

export default class CommonConst {
    public static colorObj: Object = {
        1: 'red',
        2: 'pink',
        3: 'purple',
        4: 'blue',
        5: 'teal',
        6: 'green',
        7: 'indigo',
        8: 'orange',
        9: 'brown',
    }
}

export default class Constants extends CommonConst {
    public static readonly sexList: Array<EnumVO> = [new EnumVO(1, '男'), new EnumVO(2, '女')]
    public static readonly simpleDate: string = 'YYYY-MM-DD'
    public static readonly regNotNum: RegExp = reg.regNotNum
    public static readonly routerName: any = RouterName
}



import Constants from './constants'
export default (Vue) => {
    Vue.prototype.$const = Constants
}


阅读 3k
1 个回答
public static readonly routerName: typeof RouterName = RouterName
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题