我正在尝试使用Vue.set()
来更新Vue 2中的状态对象。
这是对象的样子:
state: {
entries: [
// entry 1
fields: {
someProperties : ''
// here I would like to add another property named 'googleInfos'
}
], [
// entry 2
fields: {
someProperties : ''
// here I would like to add another property named 'googleInfos'
}
]
}
到目前为止,我一直使用这个变异来更新它。我单独地对每个条目进行变异,因为它们具有不同的内容。
ADD_GOOGLE_INFOS (state, {index, infos}) {
state.entries[index].fields.googleInfos = infos
}
现在,我正在尝试实现Vue.set()
以避免变化检测警告。
我的问题是我找不到正确的添加方式。
这是Vue.set()应该工作的方式:
Vue.set(state.object, key, value)
我尝试了这个方法,但似乎不起作用,因为state.entries[index]
不是一级对象:
Vue.set(state.entries[index], 'fields.googleInfos', infos)
但是这也行不通:
Vue.set(state.entries, '[index].fields.googleInfos', infos)
有人知道我做错了什么吗?
Vue.set(state.entries[index].fields, 'googleInfos', infos)
。 - Thomas Ferro