javascript - Mutating Arrays Vuex - Stack Overflow

The following mutation does not work:const mutations = {[types.UPDATE_IMG_URLS] (state, newArray) {stat

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 badges
Add a ment  | 

1 Answer 1

Reset to default 6

The 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745426102a4627184.html

相关推荐

  • javascript - Mutating Arrays Vuex - Stack Overflow

    The following mutation does not work:const mutations = {[types.UPDATE_IMG_URLS] (state, newArray) {stat

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信