admin管理员组文章数量:1424942
The following mutation does not work:
const mutations = {
[types.UPDATE_IMG_URLS] (state, newArray) {
state.imageUrlArray.concat(newArray)
},
(...)
}
However this one does:
const mutations = {
[types.UPDATE_IMG_URLS] (state, newArray) {
state.imageUrlArray = state.imageUrlArray.concat(newArray)
},
(...)
}
I thought arrays and objects were passed by reference in Javascript. Does Vuex meddle with this behavior?
The following mutation does not work:
const mutations = {
[types.UPDATE_IMG_URLS] (state, newArray) {
state.imageUrlArray.concat(newArray)
},
(...)
}
However this one does:
const mutations = {
[types.UPDATE_IMG_URLS] (state, newArray) {
state.imageUrlArray = state.imageUrlArray.concat(newArray)
},
(...)
}
I thought arrays and objects were passed by reference in Javascript. Does Vuex meddle with this behavior?
Share Improve this question edited Jan 26, 2017 at 19:27 softcode asked Jan 26, 2017 at 19:21 softcodesoftcode 4,68812 gold badges44 silver badges69 bronze badges1 Answer
Reset to default 6The concat
method does not change the existing array, but returns a new array. That's why you need to actively overwrite state.imageUrlArray
with the freshly returned array.
If, for some reason, you want to make the mutation in place, you should be able to to this: state.imageUrlArray(state.imageUrlArray, newArray)
See https://developer.mozilla/en/docs/Web/JavaScript/Reference/Global_Objects/Array/concat for more details.
本文标签: javascriptMutating Arrays VuexStack Overflow
版权声明:本文标题:javascript - Mutating Arrays Vuex - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745426102a2658119.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论