VSCode(Vetur) 无法识别组件中的自定义 Vue 属性

Posted

技术标签:

【中文标题】VSCode(Vetur) 无法识别组件中的自定义 Vue 属性【英文标题】:VSCode(Vetur) does not recognize customized Vue property in component 【发布时间】:2020-07-15 12:04:28 【问题描述】:

我在 Vue 上的 main.ts 文件中安装了一个插件,在 plugin.d.ts 文件中声明了它的类型,然后在 component.vue 文件中使用它。编译工作正常,但 VSCode 智能感知仍然告诉我这个属性不存在。我错过了什么吗? 这是代码。

//plugin.ts
import Vue as _Vue from 'vue'
export class Plugin  
  someFunc()  //do something  

const plugin = new Plugin()
export default function myPlugin(Vue: typeof _Vue) 
  Vue.prototye.$plugin = plugin

对于声明,

//plugin.d.ts
import  Plugin  from './plugin'
declare module 'vue/types/vue' 
  interface Vue 
    $plugin: Plugin
  

然后我将它挂载到入口点,

//main.ts
import Vue from 'vue'
import plugin from './plugin'

Vue.use(plugin)

最后,我想在组件中使用插件,

//component.vue
import  Component, Vue  from 'vue-proprety-decorator'
@Component
export default class MyComponnent extends Vue 
  func() 
    this.$plugin.someFunc()
  

编译显示没有问题,但智能感知告诉我“类型 'MyComponent' 上不存在属性 '$plugin'。”并且自动完成功能不起作用。

我做错了什么吗?

【问题讨论】:

【参考方案1】:

尝试将类型定义的名称从 plugin.d.ts 更改为 types.d.ts 。 这对我有用。

【讨论】:

非常感谢..我使用了“用于插件的增强类型”+您的提示,效果很好

以上是关于VSCode(Vetur) 无法识别组件中的自定义 Vue 属性的主要内容,如果未能解决你的问题,请参考以下文章