typescript - 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'。” 并且自动完成不起作用。
我做错什么了吗?
解决方案
尝试将类型定义的名称从 plugin.d.ts 更改为 types.d.ts 。这对我有用。
推荐阅读
- php - Laravel 使用语言环境参数返回主页
- dataweave - 在 dataweave 中加载 csv 时处理特殊字符
- xml - 如何拆分字符串并删除重复和连接字符串的第二部分
- javascript - 如何在更改之前从 Slick 查看断点?
- google-sheets - Google 表格,无效公式:需要修复条件格式?
- css - css 只改变背景,不改变边框
- javascript - 为可以发推文和基于主题标签“引用转发”的推特机器人编程。我需要哪个功能来“引用转推”
- react-native - deleteToken() 不会触发 onTokenRefresh() rnfirebase
- ruby-on-rails - Rails 6 / Administrate:安装 administrate-field-nested_has_many 时出现 Sprocket 错误
- java - 如何过滤具有前提条件的组?