Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call an action from within another action

Tags:

vue.js

vuex

People also ask

How do you call an action from another action?

You can call another Vuex action by passing the name of that action as a string as the first argument of dispatch : const store = new Vuex. Store({ actions: { walk(context) { context. dispatch("goForward"); }, goForward(context) { // }, }, });

What is VUEX in VUE JS?

Vuex is a state management pattern + library for Vue. js applications. It serves as a centralized store for all the components in an application, with rules ensuring that the state can only be mutated in a predictable fashion.


You have access to the dispatch method in the object passed in the first parameter:

get1: ({ commit, dispatch }) => {
  dispatch('get2');
},

This is covered in the documentation.


You can access the dispatch method through the first argument (context):

export const actions = {
  get({ commit, dispatch }) {
    dispatch('action2')
  }
}

However, if you use namespaced you need to specify an option:

export const actions = {
  get({ commit, dispatch }) {
    dispatch('action2', {}, { root: true })
  }
}

for actions that does not require payload

actions: {
    BEFORE: async (context, payload) => {
    },
    AFTER: async (context, payload) => {
        await context.dispatch('BEFORE');
    }
}

for actions that does require payload

actions: {
    BEFORE: async (context, payload) => {
    },
    AFTER: async (context, payload) => {
        var payload = {}//prepare payload
        await context.dispatch('BEFORE', payload);
    }
}

we can pass parameters also while dispatching.

dispatch('fetchContacts', user.uid);