Vue资源$http未定义

Vue Resource $http not defined

本文关键字:未定义 http 资源 Vue      更新时间:2023-10-03

我的ready方法中有以下代码: this.$http.get('url',{},{ headers: { "X-App-Token": "token" } }).then( (data) => this.$set('cdata',data.data)) .catch( (error) => console.log('Got a problem'+error));

它工作得很好,问题是当我把它移到方法对象中的另一个函数时,它不工作。

ready(){
this.getJsonData();
},
methods: {
getJsonData: () => {
this.$http.get('url',{},{
  headers: {
        "X-App-Token": "token"
     }
  }).then(  (data) => this.$set('cdata',data.data))
    .catch( (error) => console.log('Got a problem'+error));
},
},

错误:

src'src'App.vue:23 Uncaught TypeError: Cannot read property '$http' of undefined
//this becomes undefined.

对于任何试图解决此问题的人,您都缺少vue.use()

要解决这个问题,你需要添加

import VueResource from 'vue-resource';
Vue.use(VueResource);

对于您的main.ts,它应该都是固定的

我认为这可能是使用箭头来生成函数。可能导致该函数处于未定义的范围内?试试这个:

methods: {
  getJsonData(){
    this.$http.get('url',{},{
      //...