V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
zeronofreya
V2EX  ›  Vue.js

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

  •  
  •   zeronofreya · Aug 6, 2020 · 3643 views
    This topic created in 2101 days ago, the information mentioned may be changed or developed.

    代码为:

    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 replies    2020-08-06 11:13:26 +08:00
    troycode
        1
    troycode  
       Aug 6, 2020
    命名不规范吧,第二个参数不能直接为 vue ?
    wenhai95
        2
    wenhai95  
       Aug 6, 2020 via Android
    1.写一个.vue 文件的.d.ts 声明文件,在上面加一个 install 方法
    2.类型标注改为 typeof Vue
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3916 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 04:33 · PVG 12:33 · LAX 21:33 · JFK 00:33
    ♥ Do have faith in what you're doing.