我用的最基本的vuex方法就成功了:
this.$store.dispatch('login',data)
.then(response=>this.$router.push({name:'profile'}))
.catch(error=>console.log(error.response.data));
export default {
state:{
name:null,
email:null,
authenticated:false
},
mutations:{
login(state,payload){
state.authenticated=true;
state.name=payload.name;
state.email=payload.email;
}
},
actions:{
login({commit},payload){
axios.post(`http://vue-spa/api/login`,payload)
.then(response=>{
Cookie.setToken(response.data.access_token);
commit('login',{email:payload.email,name:response.data.name})
})
}
}
}