Vue.js方法-在lodash _each循环中访问`this`

时间:2019-06-11 19:00:05

标签: javascript vue.js lodash

我的应用有一个collaborators列表,每个列表旁边都有一个复选框。

用户可以检查多个协作者,然后单击按钮将其删除,这将触发以下Vue.js方法:

methods: {
        remove: function () {
            if (confirm('Are you sure you want to delete these collaborators?')) {
                axios.get('/collaborators/api/destroy', {
                    params: {
                        ids: this.selectedCollaborators
                    }
                })
                    .then(response => {

                        // Loop through the `selectedCollaborators` that were deleted and
                        // remove them from `collaborators`
                        _.each(this.selectedCollaborators, function (value, key) {

                            console.log('Remove collaborator: ' + value);

                            // Following line produces: TypeError: Cannot read property 'collaborators' of undefined
                            this.collaborators.splice(this.collaborators.indexOf(value), 1)

                        });
                    });

            }
        },
// [...etc...]

如您在上面的代码中看到的那样,当处理ajax响应时,我尝试使用lodash的selectedCollaborators遍历每个_each,然后从其中删除每个协作者使用拼接的collaborators数据属性。

问题是{。{each}中的this.collaborators无法访问,并且产生以下错误:

TypeError: Cannot read property 'collaborators' of undefined

我该如何解决/有没有更好的方法来解决这个问题?

2 个答案:

答案 0 :(得分:0)

您可以做的是将this保存在变量中。

methods: {
        remove: function () {
            if (confirm('Are you sure you want to delete these collaborators?')) {
                axios.get('/collaborators/api/destroy', {
                    params: {
                        ids: this.selectedCollaborators
                    }
                })
                    .then(response => {
                        const t = this;
                        // Loop through the `selectedCollaborators` that were deleted and
                        // remove them from `collaborators`
                        _.each(this.selectedCollaborators, function (value, key) {
                            console.log('Remove collaborator: ' + value);
                            t.collaborators.splice(t.collaborators.indexOf(value), 1)
                        });
                    });

            }
        },
// [...etc...]

答案 1 :(得分:0)

尝试在词汇上下文中将函数替换为箭头函数:

methods: {
    remove: () => {
        if (confirm('Are you sure you want to delete these collaborators?')) {
            axios.get('/collaborators/api/destroy', {
                params: {
                    ids: this.selectedCollaborators
                }
            })
                .then(response => {

                    // Loop through the `selectedCollaborators` that were deleted and
                    // remove them from `collaborators`
                    _.each(this.selectedCollaborators, (value, key) => {

                        console.log('Remove collaborator: ' + value);

                        // Following line produces: TypeError: Cannot read property 'collaborators' of undefined
                        this.collaborators.splice(this.collaborators.indexOf(value), 1)

                    });
                });

        }
    },