在login.vue中通过发送http请求获取token
//根据api接口获取token
var url = ‘http://www.baidudd.com’ + "/session";
this.$axios.post(url, {
username: this.loginForm.username,
password: this.loginForm.pass
}).then(res => {
// console.log(res.data);
this.$message.success('登录成功');
let data = res.data;
//根据store中set_token方法将token保存至localStorage/sessionStorage中,data["Authentication-Token"],获取token的value值
this.$store.commit('set_token', data["Authentication-Token"]);
vuex中mutations的token保存,也可以是localStorage或者是存在sessionStorage
if (store.state.token) {
this.$router.push('/')
console.log(store.state.token)
} else {
this.$router.replace('/login');
}
——关于vue-router的跳转补充说明——————————————