在vue中獲取token,并將token寫進(jìn)header的方法
需要準(zhǔn)備的東西:Vue+axios+Vuex+Vue-router
1.在login.vue中通過發(fā)送http請(qǐng)求獲取token
//根據(jù)api接口獲取token
var url = this.HOST + "/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;
//根據(jù)store中set_token方法將token保存至localStorage/sessionStorage中,data["Authentication-Token"],獲取token的value值
this.$store.commit('set_token', data["Authentication-Token"]);
if (store.state.token) {
this.$router.push('/')
console.log(store.state.token)
} else {
this.$router.replace('/login');
}
}).catch(error => {
// this.$message.error(error.status)
this.loading = false
this.loginBtn = "登錄"
this.$message.error('賬號(hào)或密碼錯(cuò)誤');
// console.log(error)
})
2.在store.js中對(duì)token狀態(tài)進(jìn)行監(jiān)管
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state:{
token:''
},
mutations:{
set_token(state, token) {
state.token = token
sessionStorage.token = token
},
del_token(state) {
state.token = ''
sessionStorage.removeItem('token')
}
}
})
3.在router/index.js中
// 頁面刷新時(shí),重新賦值token
if (sessionStorage.getItem('token')) {
store.commit('set_token', sessionStorage.getItem('token'))
}
const router = new Router({
mode: "history",
routes
});
router.beforeEach((to, from, next) => {
if (to.matched.some(r => r.meta.requireAuth)) { //這里的requireAuth為路由中定義的 meta:{requireAuth:true},意思為:該路由添加該字段,表示進(jìn)入該路由需要登陸的
if (store.state.token) {
next();
}
else {
next({
path: '/login',
query: {redirect: to.fullPath}
})
}
}
else {
next();
}
})
4.在main.js中定義全局默認(rèn)配置:
Axios.defaults.headers.common['Authentication-Token'] = store.state.token;
5.在src/main.js添加攔截器
// 添加請(qǐng)求攔截器
Axios.interceptors.request.use(config => {
// 在發(fā)送請(qǐng)求之前做些什么
//判斷是否存在token,如果存在將每個(gè)頁面header都添加token
if(store.state.token){
config.headers.common['Authentication-Token']=store.state.token
}
return config;
}, error => {
// 對(duì)請(qǐng)求錯(cuò)誤做些什么
return Promise.reject(error);
});
// http response 攔截器
Axios.interceptors.response.use(
response => {
return response;
},
error => {
if (error.response) {
switch (error.response.status) {
case 401:
this.$store.commit('del_token');
router.replace({
path: '/login',
query: {redirect: router.currentRoute.fullPath}//登錄成功后跳入瀏覽的當(dāng)前頁面
})
}
}
return Promise.reject(error.response.data)
});
以上這篇在vue中獲取token,并將token寫進(jìn)header的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue-resource + json-server模擬數(shù)據(jù)的方法
本篇文章主要介紹了vue-resource + json-server模擬數(shù)據(jù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
vue中的H5移動(dòng)端項(xiàng)目?真機(jī)測(cè)試配置方式
這篇文章主要介紹了vue中的H5移動(dòng)端項(xiàng)目?真機(jī)測(cè)試配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Vue向后臺(tái)傳數(shù)組數(shù)據(jù),springboot接收vue傳的數(shù)組數(shù)據(jù)實(shí)例
這篇文章主要介紹了Vue向后臺(tái)傳數(shù)組數(shù)據(jù),springboot接收vue傳的數(shù)組數(shù)據(jù)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Vue-resource實(shí)現(xiàn)ajax請(qǐng)求和跨域請(qǐng)求示例
本篇文章主要介紹了Vue-resource實(shí)現(xiàn)ajax請(qǐng)求和跨域請(qǐng)求示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
如何使用vuejs實(shí)現(xiàn)更好的Form validation?
如何使用vuejs實(shí)現(xiàn)更好的Form validation?這篇文章主要介紹了vue-form插件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
VUE + UEditor 單圖片跨域上傳功能的實(shí)現(xiàn)方法
這篇文章主要介紹了VUE + UEditor 單圖片跨域上傳功能的實(shí)現(xiàn)方法,需要的朋友參考下2018-02-02
優(yōu)雅的elementUI table單元格可編輯實(shí)現(xiàn)方法詳解
這篇文章主要介紹了優(yōu)雅的elementUI table單元格可編輯實(shí)現(xiàn)方法詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
vue使用高德地圖實(shí)現(xiàn)添加點(diǎn)標(biāo)記和獲取點(diǎn)擊位置信息的示例代碼
這篇文章主要介紹了vue使用高德地圖實(shí)現(xiàn)添加點(diǎn)標(biāo)記和獲取點(diǎn)擊位置信息的示例代碼,文中補(bǔ)充介紹了高德vue-amap使用(一)標(biāo)記點(diǎn)位獲取地址及經(jīng)緯度,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2024-01-01

