账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    typescript+vue如何将多个常量类挂在到vue原型的$const属性上?
    • 2019-05-19 00:00
    • 11
    39
    0

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

    我想以 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
    }
    
    
    
    2
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 共 0 条
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部