【问题标题】:Vue js "Cannot read property 'get' of undefined" on get requestVue js“无法读取未定义的属性'get'”获取请求
【发布时间】:2016-03-07 10:06:37
【问题描述】:

这个错误出现在 post 和 get 请求的日志中。我正在使用 laravel 5,并在页面底部按顺序拉入 vuejs 和 vue-resource。但由于某种原因,使用this.$http.getthis.$http.post 发出任何请求都会返回标题中的错误。

脚本

    <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);
                    });

                }
            });
        }
    }

});

【问题讨论】:

    标签: javascript laravel vue.js


    【解决方案1】:

    您正在尝试将 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);
                });
    
            }
        });
    }
    

    【讨论】:

      【解决方案2】:

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

      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);
        }
      }
      };
      

      【讨论】:

        猜你喜欢
        • 2018-05-08
        • 2021-04-29
        • 2017-04-21
        • 1970-01-01
        • 2020-10-09
        • 2021-08-28
        • 2018-03-15
        • 2019-01-30
        • 2021-08-23
        相关资源
        最近更新 更多