只是执行了一次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 || {},
};
}
},
vuex 万分强调:
不要直接在
state
上修改状态,你先是手动修改,后又通过mutation
修改,可不是增加了两次吗。