函数参数是解构出来的数组时在处理数据的时候要注意函数内部使用解构的方式对其进行添加删除操作是不能传到函数外面的,但是函数运行不会报错,
// 错误示范
addItem ({ tolist }, value) {
tolist = [value, ...tolist]
this.commit('storage')
},
// 运行以上函数会执行函数内打印正常,但是后面调用的函数拿到的tolist数据还是以前的数据
可用以下方法解决
addItem (state, value) {
state.tolist = [value, ...state.tolist]
this.commit('storage')
},
如果一定要在参数中使用解构,则函数内部就不能使用解构
addItem ({ tolist }, value) {
tolist=tolist.push(value)
this.commit('storage') // 此时调用函数,拿到的tolist就是函数处理之后的数据
},