main.js
import axios from 'axios'
axios.interceptors.request.use((config)=>{
// console.log(config);
let token=sessionStorage.getItem('token')
if(token){
config.headers.Authorization=token
}
return config
})
Vue.prototype.$http=axios
npm install jwt-decode
router
import jwt_decode from 'jwt-decode'
router.beforeEach((to, form, next) => {
if (to.path == '/login') {
next()
}
if (to.path != '/login') {
let token = sessionStorage.getItem('token');
if (!token) {
next('/login')
} else {
var { exp, name } = jwt_decode(token);
var time = new Date().getTime()
time = Math.floor(time / 1000)
if (exp - time >= 0) {
next()
} else {
next('/login')
}
}
}
})