Vue中…mapMutations传递参数

通过子组件定义的方法传递参数,在…mapMutations引用。

不多逼逼,看代码!
store文件中:

import Vuex from 'vuex';
import Vue from 'vue';
Vue.use(Vuex);
let store = new Vuex.Store({
    state: {
        name: 'hahahah',
        age: '19',
    },
    mutations: {
        changeName(state, params) {
            console.log(params);
            state.name = params.name 
        },
        changeAge(state, params) {
            state.age = params.age
        }
    },
})
export default store

VueDemo中:

<template>
  <div>
    <h4>这里是son1组件</h4>
    name:{{name}}
    age:{{age}}
    <button @click="hehe">改名字</button>
  </div>
</template>
<script>
import { mapState, mapMutations } from "vuex";
export default {
  data() {
    return {
      list: {
        name: "6666"
      }
    };
  },
  computed: {
    ...mapState(["name", "age"])
  },
  methods: {
    hehe() {
      this.changeName(this.list);
    },
    ...mapMutations(["changeName"])
  }
};
</script>
<style>
</style>

当然,也可以写直接传递

 state.age = params
<button @click="changeName(555555)">改名字</button>

省略data传参

 ...mapMutations(["changeName"])
Logo

前往低代码交流专区

更多推荐