为了账号安全,请及时绑定邮箱和手机立即绑定

vuex中使用push给数组增加数据,会增加两条相同的数据

vuex中使用push给数组增加数据,会增加两条相同的数据

慕勒3428872 2019-02-15 11:57:06
只是执行了一次push,为何会向数组里push两条相同的数据呢。百思不得其解。(this.state.updateTripInfoObj.people.data).push(obj); // 这里this.state.travellerInfo.after.temporary.people.data.push(obj); commit('setTripInfoObj', {     type: 'people',     res: this.state.updateTripInfoObj.people.data, }); commit('setTravellerInfo', this.state.travellerInfo);setTripInfoObj 只会做赋值操作setTripInfoObj(state, payload) {    if (payload.type === 'people') {         state.updateTripInfoObj.people.data = payload.res;     } else {         const { address, people, stroke, take } = payload;         state.updateTripInfoObj = {            people: people || [],            stroke: stroke || {},            address: address || [],            take: take || {},         };     } },
查看完整描述

1 回答

?
慕莱坞森

TA贡献1810条经验 获得超4个赞

vuex 万分强调:

更改 Vuex 的 store 中的状态的唯一方法是提交 mutation。

不要直接在 state 上修改状态,你先是手动修改,后又通过 mutation 修改,可不是增加了两次吗。


查看完整回答
反对 回复 2019-02-15
  • 1 回答
  • 0 关注
  • 782 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信