Answer the question
In order to leave comments, you need to log in
Why does [vuex] unknown mutation type: give me an error?
Gives an error message[vuex] unknown mutation type: setUser
const state = {
user:{
}
},
getters = {
},actions = {
getUser(){
axios.get("/api/v1/user/current")
.then(response=>{
this.commit('setUser', response.data);
})
},
loginUser({}, user){
axios.get('/sanctum/csrf-cookie').then(response => {
axios.post('/api/v1/user/login', {
email: user.email,
password: user.password
}).then(response=>{
console.log(response.data);
localStorage.setItem(
"token",
response.data.access_token
)
})
});
}
},
mutations = {
setUser(state,data){
state.user = data;
}
};
export default {
namespaced: true,
state,
getters,
actions,
mutations
}
created() {
axios.defaults.headers.common['Authorization'] = "Bearer "+localStorage.getItem('token');
this.$store.dispatch('currentUser/getUser');
}
Answer the question
In order to leave comments, you need to log in
I think this is it:
this.commit('setUser', response.data);
looking for a mutation from the root store.
try something like this:
getUser({ commit }){
axios.get("/api/v1/user/current")
.then(response=>{
commit('setUser', response.data);
})
},
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question