admin管理员组文章数量:1186262
I am trying to use Vue.set()
to update a state object in Vue 2.
This is what the object looks like:
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'
}
]
}
So far, I was updating it with this mutation. I'm mutating each entry separately because they have different content.
ADD_GOOGLE_INFOS (state, {index, infos}) {
state.entries[index].fields.googleInfos = infos
}
Now, I'm trying to implement Vue.set()
to avoid a change detection caveat.
My problem is that I can't find the proper way to add it.
Here's how Vue.set() is supposed to work :
Vue.set(state.object, key, value)
So I tried this, which doesn't seem to work because state.entries[index]
is not an object of first rank :
Vue.set(state.entries[index], 'fields.googleInfos', infos)
But this doesn't work either :
Vue.set(state.entries, '[index].fields.googleInfos', infos)
Any one has a clue what I'm doing wrong ?
I am trying to use Vue.set()
to update a state object in Vue 2.
This is what the object looks like:
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'
}
]
}
So far, I was updating it with this mutation. I'm mutating each entry separately because they have different content.
ADD_GOOGLE_INFOS (state, {index, infos}) {
state.entries[index].fields.googleInfos = infos
}
Now, I'm trying to implement Vue.set()
to avoid a change detection caveat.
My problem is that I can't find the proper way to add it.
Here's how Vue.set() is supposed to work :
Vue.set(state.object, key, value)
So I tried this, which doesn't seem to work because state.entries[index]
is not an object of first rank :
Vue.set(state.entries[index], 'fields.googleInfos', infos)
But this doesn't work either :
Vue.set(state.entries, '[index].fields.googleInfos', infos)
Any one has a clue what I'm doing wrong ?
Share Improve this question edited Jul 14, 2022 at 1:21 tony19 138k23 gold badges276 silver badges346 bronze badges asked May 2, 2018 at 11:44 Marine Le BorgneMarine Le Borgne 1,0562 gold badges11 silver badges39 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 28The only non-reactive part is the new property you try to add in the fields
object.
In order to add the googleInfos
property, you have to set it in the mutation like this :
ADD_GOOGLE_INFOS (state, {index, infos}) {
Vue.set(state.entries[index].fields, 'googleInfos', infos);
}
本文标签: javascriptUsing Vueset in object with multiple nested objectsStack Overflow
版权声明:本文标题:javascript - Using Vue.set in object with multiple nested objects - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738348384a2078504.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Vue.set(state.entries[index].fields, 'googleInfos', infos)
– Thomas Ferro Commented May 2, 2018 at 11:47