V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zeronofreya
V2EX  ›  Vue.js

请问 vue Typescript 编写组件库报错 Property 'component' does not exist on type 'Vue'.应该怎么解决?

  •  
  •   zeronofreya · 2020-08-06 10:42:02 +08:00 · 2895 次点击
    这是一个创建于 1331 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码为:

    import VueXxx from './src/VueXxx.vue'
    import Vue from 'vue'
    VueXxx.install = (vue: Vue) => {
      vue.component(VueXxx.name, VueXxx)
    }
    
    export default VueXxx
    

    报错:

    Property 'install' does not exist on type 'VueConstructor<Vue>'.ts(2339)
    Property 'component' does not exist on type 'Vue'.ts(2339)
    

    我改成any就不报错了:

    (VueXxx as any).install = (vue: any) => {
      vue.component(VueXxx.name, VueXxx)
    }
    
    

    请问,只能这么做吗?

    2 条回复    2020-08-06 11:13:26 +08:00
    troycode
        1
    troycode  
       2020-08-06 11:00:28 +08:00
    命名不规范吧,第二个参数不能直接为 vue ?
    wenhai95
        2
    wenhai95  
       2020-08-06 11:13:26 +08:00 via Android
    1.写一个.vue 文件的.d.ts 声明文件,在上面加一个 install 方法
    2.类型标注改为 typeof Vue
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2735 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 12:36 · PVG 20:36 · LAX 05:36 · JFK 08:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.