Search code examples
vue.jsvuex

Vuejs simplify vuex action call


I am calling a store action through a component like:

sublitPost(){
    this.$store.dispatch({type:"submitPost", userPost:this.newPost});
}

and the store action looks like this:

  actions: {
    submitPost({commit, state}, userPost: any) {
     /...rest code here

Although, I would like to simplify this and call the action like:

sublitPost(){
    this.$store.dispatch("submitPost", this.newPost);
}

What is the tweak in the action's signature I have to do? What I've tried is to have the action like:

  actions: {
        submitPost(userPost: any) {
          console.log({userPost})
         /...rest code here

but in this case, the received object doesn't look correct. What I get in the console log is: enter image description here

Any help is welcome


Solution

  • You should change your syntax:

    actions: {
        submitPost(state, data) {
          console.log(data)
        {