首页 文章

VueJS:未捕获(在承诺中)TypeError:无法读取未定义的属性'push'

提问于
浏览
5

我得到'无法读取属性推送未定义'错误:这是我的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);
                });
            });

我究竟做错了什么?谢谢

2 回答

  • 0

    this.CoIsignedListUID 未定义

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

    你应该做

    var _this = this
    

    在功能之外然后

    _this.CoIsignedListUID.push(detail.uID);
    

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

    代替:

    .then(function (response) {}
    

    使用:

    .then((response) => {}
    

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

  • 8

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

相关问题