Do not mutate vuex store state outside mutation handlers.

今天遇到一个问题,将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。

《Do not mutate vuex store state outside mutation handlers.》

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

    原文作者:木子喵
    原文地址: https://segmentfault.com/a/1190000017383271
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞