简体   繁体   English

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

[英]VSCode(Vetur) does not recognize customized Vue property in component

I've mounted a plugin on Vue in a main.ts file, declared its type in a plugin.d.ts file, then used it in a component.vue file.我在 Vue 上的 main.ts 文件中安装了一个插件,在 plugin.d.ts 文件中声明了它的类型,然后在 component.vue 文件中使用它。 The compilation works fine, but VSCode intellisense still tells me this property does not exist.编译工作正常,但 VSCode 智能感知仍然告诉我这个属性不存在。 Am I missing anything?我错过了什么吗? Here are the codes.这是代码。

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

and for declaration,和声明,

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

then I mount it in an entry point,然后我将它安装在一个入口点,

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

Vue.use(plugin)

finally, I want to use the plugin in a component,最后,我想在组件中使用插件,

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

The compilation shows no problem, but intellisense tells me "Property '$plugin' does not exist on type 'MyComponent'."编译显示没有问题,但智能感知告诉我“类型 'MyComponent' 上不存在属性 '$plugin'。” and autocompletion does not work.并且自动完成不起作用。

Did I do anything wrong?我做错什么了吗?

Try to change the name of the type definition from plugin.d.ts to types.d.ts.尝试将类型定义的名称从 plugin.d.ts 更改为 types.d.ts。 That did the trick for me.这对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NativeScript-Vue with Typescript in VSCode with Vetur: Alert with then giving Property 'then' does not exist on type 'void' - NativeScript-Vue with Typescript in VSCode with Vetur: Alert with then giving Property 'then' does not exist on type 'void' TypeScript + VSCode 的 Vetur Vue package:找不到名称“HTMLElement”/“窗口”/“文档” - TypeScript + VSCode's Vetur Vue package: Cannot find name 'HTMLElement' / 'window' / 'document' VsCode 插件错误 'Property'X' 在 Vue 3 模板中的类型'{}' 上不存在 - VsCode plugin error 'Property 'X' does not exist on type '{}' in Vue 3 template Angular 子组件无法识别输入属性的变化 - Angular child component does not recognize change in input property Typescript组件可以正常运行,但是会出现错误,因为它无法识别那里的属性 - Typescript component runs correctly but says there is an error because it does not recognize a property that is there Vue JS Typescript 项目。 试图减少每个组件中的 Model 进口,但 Vetur 抱怨 - Vue JS Typescript Project. Trying to reduce Model Imports in each component but Vetur Complains 如何解决 Vetur/Vuelidate 错误“&#39;验证&#39;在&#39;ComponentOptions 类型中不存在&#39; - How can I resolve Vetur/Vuelidate error “'validate' does not exist in type 'ComponentOptions<Vue [etc.]”? Vue 组件中缺少 this 的属性 - Missing Property of this in Vue component VSCode 不识别 typescript 3.7.2 功能 - VSCode does not recognize typescript 3.7.2 features Typescript 无法识别解构属性 - Typescript does not recognize a destructured property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM