vuex 的严格模式strict: true下,不允许组件修改state中的值,若需要修改,在vuex的mutations属性中进行修改。
注意:只能在mutations下的函数进行修改,如果mutations下的函数的函数进行修改也是会报错的
(1)mutations属性中接收两个参数,state就是vuex中的state,addValue是组件传过来的值
mutations: { changePersonList (state, addValue) { state.personList.push(addValue) } },
(2)组件中:add函数触发changePersonList函数,并将this.addValue的值传过去。
add () { this.$store.commit('changePersonList', this.addValue) }
(3)如果穿多个值呢?由于vuex中mutations属性中的函数,只能接收两个参数,所以:第二个参数用对象接收
add () { this.$store.commit('changePersonList', {a: 1, b: 2}) }
mutations: { changePersonList (state, {a, b}) { state.personList.push() } },
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/215185.html原文链接:https://javaforall.net
