关于vue.js:VSCode和Vue 2插件TypeScript定义

VSCode and Vue 2 plugin TypeScript definitions

我正在使用VSCode和TypeScript类进行Vue 2组件开发。请参阅:vuejs / vue-class-component。

在我当前的项目中,我使用诸如vue-i18n之类的插件来翻译标签等。这些插件通过诸如this.$t(...)之类的自身功能扩展了Vue组件,以获得一个通过键进行翻译,但是VSCode无法识别/不能不退出这些扩展(或者它们是否是混合插件?)等。

我如何才能了解到这些扩展功能存在且intellisense开始工作的VSCode?我可以创建自己的* .d.ts文件吗?如果是这样,我如何将它们连接起来,以便VSCode可以找到它们以进行智能检测?任何例子都欢迎。或链接到一些示例Github存储库中已完成的操作?


此问题现在已解决,并记录在Vue TypeScript文档中。它称为\\\\"与插件一起使用的扩展类型"。

以下摘录自该页面,供快速参考:

1
2
3
4
5
6
7
8
9
10
11
12
13
// For example, to declare an instance property $myProperty with type string:

// 1. Make sure to import 'vue' before declaring augmented types
import Vue from 'vue'

// 2. Specify a file with the types you want to augment
//    Vue has the constructor type in types/vue.d.ts
declare module 'vue/types/vue' {
  // 3. Declare augmentation for Vue
  interface Vue {
    $myProperty: string
  }
}

vue-i18n不提供自己的TypeScript类型。相反,您可以使用DefinitelyTyped:

npm i -D @types/vue-i18nyarn add -D @types/vue-i18n