iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📖

Vuex Store Arguments | Vue 3 + Vuex 4

に公開
2024/04/09

Mutations

How to Define

index.js
state: {
    scale: 0,
},
mutations: {
    // No arguments
    increment(state) {
        state.scale++;
    },
    // 1 argument
    plus(state, plus) {
        state.scale += plus;
    },
    // 2 or more arguments
    plusMinus(state, { plus, minus }) {
        state.scale = scale + plus - minus;
    },
},

How to Use

SampleComponent.js
methods: {
    doIt() {
        this.$store.commit('increment');
        this.$store.commit('plus', 10);
        this.$store.commit('plusMinus', { plus: 10, minus: 5 });
    },
},

Getters

How to Define

index.js
state: {
    scale: 0,
},
getters: {
    // No arguments
    counter(state) {
        return state.scale;
    },
    // With arguments *Method-style access
    getCalcedCounter: (state) => (plus) => {
        return state.scale + plus;
    },
    // (Reference) Using other getters
    counterX(state, getters) {
        const current = getters.counter; // ← state.scale
        return current;
    },
},

How to Use

SampleComponent.js
methods: {
    doIt() {
        this.$store.getters.counter;
        this.$store.getters.getCalcedCounter(10);
                                // ^^^^^^^^^^^^^^^^
                                // Note that it is a method
        this.$store.getters.counterX;
    },
},

Actions

How to Define

The following assumes having the mutations defined above.

index.js
state: {
    scale: 0,
},
actions: {
    // No arguments 
    doIncrement({ commit }) {
        commit('increment');
    },
    // 1 argument
    doPlus({ commit }, plus) {
        commit('plus', plus);
    },
    // 2 or more arguments
    doPlusMinus({ commit }, { plus, minus }) {
        commit('plusMinus', { plus, minus });
    },
    // (Reference) Using other actions
    doOtherAction({ dispatch, commit }) {
        dispatch('doIncrement');
        commit('increment');
    },
},

How to Use

SampleComponent.js
methods: {
    doIt() {
        this.$store.dispatch('doIncrement');
        this.$store.dispatch('doPlus', 10);
        this.$store.dispatch('doPlusMinus', { plus: 10, minus: 5 });
        this.$store.dispatch('doOtherAction');
    },
},

Supplement

The first argument of an Actions handler is a (Vuex) Context object, which has the following keys.

{
  state,
  rootState,
  commit,
  dispatch,
  getters,
  rootGetters
}

https://qiita.com/after666/items/7e94d61fed89406ae59a

Differences Between Mutations and Actions

Mutations are synchronous, but Actions can contain arbitrary asynchronous operations.
If an Action contains asynchronous operations and you want to know when it completes, it must return a Promise.
Note that dispatch can handle Promises.

actions: {
    // Asynchronous operation
    actionA ({ commit }) {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                commit('someMutation');
                resolve();
            }, 1000);
        });
    },
    // Calling actionA
    actionB ({ dispatch, commit }) {
        return dispatch('actionA').then(() => {
            commit('someOtherMutation');
        }),
    }
}

References

https://vuex.vuejs.org/ja/guide/mutations.html
https://vuex.vuejs.org/ja/guide/getters.html
https://vuex.vuejs.org/ja/guide/actions.html

Discussion