我使用包含类型的vue。但是我想使用某些插件添加的一些属性。
例如
Vue.$ga
Vue.$router
Run Code Online (Sandbox Code Playgroud)
Typescript抱怨Vue没有这些属性。我可以以某种方式将这些全局添加到Vue定义中吗?
是的你可以:
import Vue from 'vue'
declare module 'vue/types/vue' {
interface VueConstructor {
$ga: any; // define real typings here if you want
$router: any;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到特定于Vue的更多信息
小智 5
在 main.ts 上,添加:
Vue.prototype.$ga = 'your ga service'
Run Code Online (Sandbox Code Playgroud)
在您的项目 /src 文件夹中创建一个定义文件“my-file.d.ts”。
import Vue, { VueConstructor } from 'vue';
declare module 'vue/types/vue' {
interface Vue {
$ga: any;
}
interface VueConstructor {
$ga: any;
}
}
declare module 'vue/types/options' {
interface ComponentOptions<V extends Vue> {
$ga?: any;
}
}
Run Code Online (Sandbox Code Playgroud)
现在您可以通过简单的操作在您的组件中使用它:
console.log(this.$ga); // your ga service
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1660 次 |
| 最近记录: |