【问题标题】:Vue.http causes error when using typescriptVue.http 在使用 typescript 时导致错误
【发布时间】:2018-04-05 20:18:03
【问题描述】:

所以我刚刚将我的项目从 js 更改为 typescript,并且我有以下代码来设置 vue 资源:-

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import VueResource from 'vue-resource'

Vue.use(VueResource)
Vue.http.headers.common['Content-Type'] = 'application/json'

但是 typescript 在.http 上抛出错误。有没有标准的方法来解决这个问题?有没有办法让 typescript 知道它的存在?

在其他文件中,他们也可以访问Vue.http,但只能通过导入 Vue 来访问 http

错误是:-

或在编译器中

【问题讨论】:

  • 你还在导入Vue吗?
  • 是的,更新代码...
  • 什么是逐字错误信息?
  • @stealththeninja 错误截图....

标签: typescript vue.js


【解决方案1】:

Vue.js 在项目中包含类型定义 (types/vue.d.ts),但 VueResource 似乎还没有。首先,我认为创建自定义 vue-resource.d.ts(或 vue-resource.types.ts 并导入它),其定义扩展 Vue 的类型并将其添加到您的项目中可以解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 2023-04-07
    • 2015-10-03
    • 2018-11-23
    • 2022-12-14
    • 2018-08-24
    相关资源
    最近更新 更多