我正在一个使用 vue-resource 的 Vue 项目上使用 Typescript。该项目工作正常,但我收到 typescript 编译器错误。
请注意,我可以使用 this.$http
使用该模块在我的组件中。
我查过this , this ,和 this ,答案但这些都没有帮助。有解决办法吗?或者有没有办法使用 Typescript 在 Vue-Resource 中设置全局选项?或者完全忽略错误的方法?
[编辑]解决方法(也作为我的答案附加):
@Styx 评论中的解决方法之一(Github 解决方案非常 hacky,因为我避免手动对 vue-resource/types/vue.d.ts
进行修改),无论如何都在这里提到。
使用泛型并将 Vue 类型转换为任何类型,例如 <any>Vue.http
。然而,这可能会导致 tslint 警告,因此我通过使用 (Vue as any)
解决了这个问题然后访问(Vue as any).http
最佳答案
我认为您需要在 main.ts 所在位置添加一个名为“http.d.ts”的新定义文件以及以下内容:
import Vue from 'vue';
declare module 'vue/types/vue' {
interface VueConstructor {
http: any;
}
}
这应该可以解决错误。
关于typescript - Vue-资源 + typescript : Property 'http' does not exist on type 'VueConstructor<Vue>' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54826912/