首页 > 解决方案 > VSCode(Vetur) 无法识别组件中的自定义 Vue 属性

问题描述

我在 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'。” 并且自动完成不起作用。

我做错什么了吗?

标签: typescriptvue.jsvisual-studio-codevue-componentintellisense

解决方案


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


推荐阅读