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。

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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理