Do not mutate vuex store state outside mutation handlers.

8次阅读

共计 509 个字符,预计需要花费 2 分钟才能阅读完成。

今天遇到一个问题,将 Vuex 中数组的值赋给新的数组,新数组 push 时报上面的错误,代码如下
this.maPartListTable = this.$store.state.vehicleMa.maPartListTable;
this.maPartListTable.push(obj);
经询问后得知,应该这么写 this.maPartListTable = this.$store.state.vehicleMa.maPartListTable.slice();
查了下,就查到这句 slice(),不会修改原始数组而是返回一个新数组
操作是这样,每次赋值新数组(selection)给 temp, 然后 actions 中 commit motations 改变 state 值 currentseletedRows 就报这个错。解决:如图,加个 slice。

思考:引用数据类型,vuex 里的 currentSelectedRows 引用 selection 变量,如果不加 slice, 改变 selection 就直接改变 vuex 里的 state, 而 vuex 不允许直接改变 state 中的东西,必须通过 mutations。所以报错!!(欢迎讨论,自己目前的思考,不一定对)

正文完
 0