【问题标题】:Vue Resource $http not definedVue 资源 $http 未定义
【发布时间】:2026-01-19 08:20:04
【问题描述】:

我的 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.

【问题讨论】:

    标签: javascript vue.js vue-resource


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        最近更新 更多