Vuex - 更新数组内的整个对象 [英] Vuex - Update entire object inside array

查看:28
本文介绍了Vuex - 更新数组内的整个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Vuex突变中,我想替换我状态中的一个数组元素,如下图:

Inside my Vuex mutation, I want to replace an array element in my state, as shown below:

UPDATE_MAILING(state, mailing) {
    let index = _.findIndex(state.mailings, {id: mailing.id});

    state.mailings[index] = mailing
}

但这不会更新绑定到此数组的模板.如何被动更新数组元素?

But this does not update my template bound to this array. How can I reactively update the array element?

推荐答案

你应该使用 Vue.$set(或 this.$set 在 Vue 实例中):

You should use Vue.$set (or this.$set inside Vue instance):

UPDATE_MAILING(state, mailing) {
    let index = state.mailings.findIndex(item => item.id === mailing.id)
    Vue.$set(state.mailings, index, mailing)
}

文档:Vue.js →深度反应

这篇关于Vuex - 更新数组内的整个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆