【问题标题】:VueJS: Uncaught (in promise) TypeError: Cannot read property 'push' of undefinedVueJS:未捕获(承诺中)TypeError:无法读取未定义的属性“推送”
【发布时间】:2016-11-22 09:40:08
【问题描述】:

我收到“无法读取未定义的属性推送”错误:这是我的 vueJs 代码:

data:{
CoIsignedListUID:[]
}
methods:{

fetchCoISigned: function () {
            this.$http.get('/cimsm/public/api/fetchCoIsigned/' + this.conflictofInterest.complaintID).then(function (response) {
                var data = response.data;
                this.$set('CoIsignedList', data);
                data.forEach(function (detail) {
                    this.CoIsignedListUID.push(detail.uID);
                });
            });

我做错了什么?谢谢

【问题讨论】:

  • 试试这个:this.$data.CoIsignedListUID.push 而不是 this.CoIsignedListUID.push

标签: laravel-5 vue.js


【解决方案1】:

this.CoIsignedListUID 未定义

可能是因为this 不是你认为的this

你应该这样做

var _this = this

函数外然后

_this.CoIsignedListUID.push(detail.uID);

或者,您可以使用 ES2015 箭头语法。

代替:

.then(function (response) {}

用途:

.then((response) => {}

“this”现在在函数内部可用,因此无需创建新变量。详细信息Here

【讨论】:

    【解决方案2】:

    forEach 回调中的this 不是 vue.js 中的this。 你可以这样做来解决这个问题。

    this.$http.get("...").then(function(response){
        var data = response.data;
        this.$set('CoIsignedList', data);
        var that = this;
        data.forEach(function (detail) {
            that.CoIsignedListUID.push(detail.uID);
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2021-05-14
      • 2019-05-16
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 2020-09-24
      • 1970-01-01
      相关资源
      最近更新 更多