Laravel Vue SPA - Vuex 代码回顾
打赏作者

uighurbabbage

哈哈 我是不是跟你发布的节奏保持一致了 刚没有看完上一集 这一集就出现了

uighurbabbage

现在这方式的jwt 我担忧遇到爬虫就麻烦了 只能老老实实的等待tocken过时

liujun

我用的最基本的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})
            })
    }
}

}

liujun

我在后端proxy里面加了name一并返回,其他什么都不用做。