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

2020-08-06 10:42:02 +08:00
 zeronofreya

代码为:

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)
}

请问,只能这么做吗?

2934 次点击
所在节点    Vue.js
2 条回复
troycode
2020-08-06 11:00:28 +08:00
命名不规范吧,第二个参数不能直接为 vue ?
wenhai95
2020-08-06 11:13:26 +08:00
1.写一个.vue 文件的.d.ts 声明文件,在上面加一个 install 方法
2.类型标注改为 typeof Vue

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/696066

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX