Mar*_*gne 9 javascript ecmascript-6 vue.js vuejs2
我试图用来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'
}
]
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我正在用这个突变更新它.我正在分别改变每个条目,因为它们有不同的内容.
ADD_GOOGLE_INFOS (state, {index, infos}) {
state.entries[index].fields.googleInfos = infos
}
Run Code Online (Sandbox Code Playgroud)
现在,我正在尝试实施Vue.set()
以避免更改检测警告.
我的问题是我找不到合适的方法来添加它.
这是Vue.set()应该如何工作:
Vue.set(state.object, key, value)
Run Code Online (Sandbox Code Playgroud)
所以我尝试了这个,这似乎不起作用,因为state.entries[index]
它不是第一级的对象:
Vue.set(state.entries[index], 'fields.googleInfos', infos)
Run Code Online (Sandbox Code Playgroud)
但这也不起作用:
Vue.set(state.entries, '[index].fields.googleInfos', infos)
Run Code Online (Sandbox Code Playgroud)
任何人都知道我做错了什么?
Tho*_*rro 15
唯一的非反应部分是您尝试在fields
对象中添加的新属性.
要添加googleInfos
属性,您必须在突变中设置它,如下所示:
ADD_GOOGLE_INFOS (state, {index, infos}) {
Vue.set(state.entries[index].fields, 'googleInfos', infos);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5607 次 |
最近记录: |