typescript - Vue-资源 + typescript : Property 'http' does not exist on type 'VueConstructor<Vue>'

标签 typescript vue.js vue-resource

我正在一个使用 vue-resource 的 Vue 项目上使用 Typescript。该项目工作正常,但我收到 typescript 编译器错误。

enter image description here enter image description here

请注意,我可以使用 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/

相关文章:

python - 如何将 JSON 数据从 Django View 传递到 Vue.js 实例方法

javascript - 如何使用 Vue.js 访问 API?

javascript - 如何解决 Vetur/Vuelidate 错误 "' 验证'在类型 'ComponentOptions<Vue [etc.]"中不存在?

angular - 在 ng-bootstrap Angular 2 typescript 中的选项卡之间切换时如何保留以前的 ngbTab 内容

typescript - 从参数元组中删除最后一个元素,取消最后一个参数

javascript - 使用 jQuery 加载脚本后无法使用 javascript 函数

javascript - 如何在 Vuetify 中显示 <v-text-field> 的动态占位符文本?

javascript - GET http://127.0.0.1:8000/dist/bundle.js net::ERR_ABORTED 404 (Not Found),尽管编译成功。(vuejs+django webapp)

javascript - 响应式(Reactive)更新 Vue JS 中的资源?

php - Vue 资源发布请求内容类型