个人愚见--->
再写了一个action和mutation,用window.localstorage.getItem()来判断用户是否登录(用户登录的时候把name,email也存到storage里面),如下:
App组件:
created(){
if(Cookie.getToken()) this.$store.dispatch('loginRefresh')
}
store:
mutations:{
login(state,payload){
state.authenticated=true;
state.name=payload.name;
state.email=payload.email;
},
loginRefresh(state){
state.authenticated=true;
state.name=window.localStorage.getItem('name');
state.email=window.localStorage.getItem('email');
}
},
actions:{
login({commit},payload){
axios.post(`http://vue-spa/api/login`,payload)
.then(response=>{
Cookie.setToken(response.data.access_token);
window.localStorage.setItem('name',response.data.name);
window.localStorage.setItem('email',payload.email);
commit('login',{name:response.data.name,email:payload.email})
})
},
loginRefresh({commit}){
commit('loginRefresh')
}
}