1

我在准备好的方法中有这段代码: 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.
4

2 回答 2

1

对于任何试图解决这个问题的人,你都错过了vue.use()

要解决此问题,您需要添加

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

到你的 main.ts ,它应该都是固定的

于 2018-06-15T21:07:03.577 回答
0

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

methods: {
  getJsonData(){
    this.$http.get('url',{},{
      //...
于 2016-03-31T15:10:26.337 回答