Vue js "Cannot read property 'get' of undefined" 获取请求

Vue js "Cannot read property 'get' of undefined" on get request

此错误出现在 post 和获取请求的日志中。我正在使用 laravel 5,并在页面底部按顺序引入了 vuejs 和 vue-resource。但出于某种原因,使用 this.$http.getthis.$http.post returns 提出任何请求时,标题中出现错误。

脚本

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.10/vue.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue-resource/0.7.0/vue-resource.min.js"></script>
    <script src="js/jquery.easing.min.js"></script>
    <script src="js/scrolling-nav.js"></script>
    <script src="js/app.js"></script>

Js/app.js代码

new Vue({

    el: '#application',

    data: {

    },

    ready: function(){
        this.openModal();
    },

    methods: {
        openModal: function(){
            $('td').on('click', function(){
                if($(this).hasClass('not-month'))
                {

                }
                else
                {
                    var year = '2016';
                    var month = $(this).parent().parent().parent().parent().find('h4').text();
                    var day = $(this).text();
                    console.log(day + ' ' + month + ' ' + year);

                    this.$http.get('/test', function(response){
                       this.$set('test', response);
                    });

                }
            });
        }
    }

});

您正在尝试将 this 用于 jQuery 回调。您需要先参考它并像这样使用参考:

openModal: function(){
    var instance = this;
    $('td').on('click', function(){
        if($(this).hasClass('not-month'))
        {

        }
        else
        {
            var year = '2016';
            var month = $(this).parent().parent().parent().parent().find('h4').text();
            var day = $(this).text();
            console.log(day + ' ' + month + ' ' + year);

            instance.$http.get('/test', function(response){
               instance.$set('test', response);
            });

        }
    });
}

在您的组件导出默认值中粘贴以下代码。

import _Vue from 'vue';
import _VueResource from 'vue-resource';

// vue use vueResource for http request.
_Vue.use(_VueResource);

export default {
name :  'SearchField',
_this: this,
data() {
  return {
    contactNumber: []
  }
},
methods: {
  GetContact: function () {
    // GET /someUrl
    return this.$http.get('localhost:8081').then(response => {

      // get body data
      let someData = response.body;
      alert(someData);

    }, response => {
      // error callback
      alert("error")
    }).bind(_this);
  }
}
};