vuex的mutations

一时失言乱红尘 2022-02-27 08:36 322阅读 0赞

vuex 的严格模式strict: true下,不允许组件修改state中的值,若需要修改,在vuex的mutations属性中进行修改。

注意:只能在mutations下的函数进行修改,如果mutations下的函数的函数进行修改也是会报错的

(1)mutations属性中接收两个参数,state就是vuex中的state,addValue是组件传过来的值

  1. mutations: {
  2. changePersonList (state, addValue) {
  3. state.personList.push(addValue)
  4. }
  5. },

(2)组件中:add函数触发changePersonList函数,并将this.addValue的值传过去。

  1. add () {
  2. this.$store.commit('changePersonList', this.addValue)
  3. }

(3)如果穿多个值呢?由于vuex中mutations属性中的函数,只能接收两个参数,所以:第二个参数用对象接收

  1. add () {
  2. this.$store.commit('changePersonList', {a: 1, b: 2})
  3. }
  4. mutations: {
  5. changePersonList (state, {a, b}) {
  6. state.personList.push()
  7. }
  8. },

发表评论

表情:
评论列表 (有 0 条评论,322人围观)

还没有评论,来说两句吧...

相关阅读