vue-admin-template处理token失效

内容分享2小时前发布
0 0 0

vue-admin-template处理token失效

token失效后应该删除token使其跳转至登录页面,而不是报401错误。

1.在request.js的拦截器的位置添加401的判断,在此进行token的删除和跳转

跳转要导入路由

import { router } from '@/router'

vue-admin-template处理token失效

async(error) => {
    if (error.response.status == 401) {
    Message({ type: 'warning', message: 'token 超时了' })
    // 说明token超时了
    await store.dispatch('user/logout') // 相当于调用action退出登陆
    router.push('/login') // 跳转到登录页
    }
    Message({ type: 'info', message: error.message })
    return Promise.reject(error)
    }

2.在store/user.js添加token删除action logout

vue-admin-template处理token失效

logout(context) {
    context.commit("removeToken") // 删除token
    context.commit("setUserInfo",{}) // 设置用户信息为空
}

vue-admin-template处理token失效

添加removeToken mutations

removeToken(state) {

    // 删除vuex的token

    state.token = null

    removeToken()

},

© 版权声明

相关文章

暂无评论

none
暂无评论...