methods: {
submitForm() {
this.$api
.login({ mobile: "yunfeng", password: "123456" })
.then((res) => {
if (res.status === 200) {
// 在 sessionStorage中设置token,值为后台数据中的token
sessionStorage.setItem('token',res.data.data.token) //重点
this.$store.commit("changeUser", res.data.data);
this.$store.commit("changeFlag", true);
this.$router.push({ name: "order" });
} else {
console.log(res.data.info);
}
})
.catch((data) => {
console.log(data);
});
},
(在api/index.js文件中)
const myPost = axios.create({
baseURL: urls.baseUrl,
method: 'post',
headers: {
'token': sessionStorage.getItem("token")
},
})
const myGet = axios.create({
baseURL: urls.baseUrl,
method: 'get',
})
export default {
login(obj) {
return myPost({
url: urls.login,
params: {
...obj
},
// 要在这里写 headers,从sessionStorage中拿,并给headers设置token
headers: { //重点
'token': sessionStorage.getItem("token")
},
})
},
info() {
return myGet({
url: urls.info,
headers: {
'token': sessionStorage.getItem("token")
},
})
},
}