4

私は Vues.js の初心者で、mapMutation メソッドについて質問しています。このメソッドでパラメータを渡したいのですが、方法がわかりません。mapMutations を使用して 2 つのメソッドを変換したい:

increment() {
   this.$store.commit(M_COUNT_INCREMENT, {
      amount: 1,
   });
},
decrement() {
   this.$store.commit(M_COUNT_DECREMENT, {
       amount: 1,
   });
},

私はこのようなことをしたいのですが、「金額」パラメーターを渡して:

...mapMutations({
   increment: M_COUNT_INCREMENT,
   decrement: M_COUNT_DECREMENT,
}),

何か案が ?

ありがとう

4

5 に答える 5

0

this.DECREMENT({minus: 2})原因になりthis.$store.commit('DECREMENT', obj)ます。ローカルmethods: increment()app.vue で使用

<template>
      <div id="app">
        <p>{{count}}</p>
        <button @click="INCREMENT">+</button>
        <button @click="decrement">-</button>
      </div>
    </template>

    <script>
    import { mapMutations } from "vuex";
    import { mapState } from "vuex";

    export default {
      computed: mapState(["count"]),

      methods: {
        decrement() {
          this.DECREMENT({ minus: 2 })
        },

        ...mapMutations(["INCREMENT", "DECREMENT"])
      }
    };
    </script>

store.js

export const store = () => {
  return new Vuex.Store({
    state: {
      count: 0,
    },

    mutations: {

      INCREMENT (state) {
        state.count++;
      },

      DECREMENT (state, obj) {
        state.count -= obj.minus;
      }
    },
};
于 2019-09-24T07:20:54.540 に答える