el-form组件+token验证
el-form使用
token验证
(1)从后台拿到数据后将token值储存到浏览器本地储存当中
window.sessionStorage.setItem('token', data.data.token)
(2)在main.js中导入axios时,配置好请求头头中自动携带浏览器本地储存中的token值
axios.interceptors.request.use(function(config) {
config.headers.Authorization = window.sessionStorage.token
return config
})
(3)根据本地是否有无token值,在router下index.js中进行路由拦截
router.beforeEach(function(to, from, next) {
// console.log(to)
// console.log(from)
// console.log(next)
// console.log(window.sessionStorage.token)
if (to.path === '/login') {
next()
} else {
if (window.sessionStorage.token) {
next()
} else {
next('/login')
}
}
})