[Vuejs]-Vue – how to pass callback to vuex action

4πŸ‘

βœ…

The issue takes place in 2 places:

  1. the payload syntax in your store is wrong
  2. you are firing the functions with the () at the end when passing it through an object:

Solve the payload issue

An action has 2 parameters, first comes the context which is an object that contains the state, mutations etc. then comes the payload which is an object aswell

const actions = {
  async test(context, {confirmCallback, rejectCallback}) {
    confirmCallback();
    rejectCallback();
  }
}

Solve the decleration issue
To solve the decleration issue simply remove the () at the end like shown below:

  async created() {
    let result = await this.test({
      confirmCallback: this.onModalOpen,
      rejectCallback: this.onModalClose,
    });
  },
πŸ‘€Markiesch

Leave a comment