Commit undefined error when using vuex

Reasons for occurrence:

  • written error
  • Of course, this error is not your writing error
  • As long as the reason is because there is no commit method in your method
  • Why not? In fact, it does exist, only in the context
  • You may have written commit (xxx) directly

Error display process:

First we call

<div><input type="button" @click="$store.dispatch('listMore')" value="加载更多"></div>
  • Why is dispatch because you are not using commit?
  • This is to call the methods in mutations in actions

Then there is the code:

actions = {
     login(context){
            commit("CHANGE_USERNAME")
        }
    }
};
  • You can see that a parameter was accepted, this parameter is passed by default, you can not change
  • commit is in the context, but you write directly

It can be written like this if called correctly :

actions = {
     login(context){
            context.commit("CHANGE_USERNAME")
        }
    }
};

Or like this:

actions = {
     login({commit}){  //加 { }
            commit("CHANGE_USERNAME")
        }
    }
};
Published 74 original articles · praised 7 · visits 2201

Guess you like

Origin blog.csdn.net/qq_44163269/article/details/105268508