vue 統一處理token失效問題


使用http response 攔截器 在main.js中添加

import axios from 'axios';

  

axios.interceptors.response.use(response => {
  if (response) {
    switch (response.data.code) {
      case 403: //與后台約定登錄失效的返回碼,根據實際情況處理
        localStorage.clear();     //刪除用戶信息
        //如果超時就處理 ,指定要跳轉的頁面(比如登陸頁)
        Message({
          message: 'token失效,請重新登錄!',
          type: 'warning',
          center: true,
          onClose:function () {
            console.log('關閉了看下')
            router.replace({
              path: '/user/login',
            })
          }
        });
    }
  }
  return response;
}, error => {
  return Promise.reject(error.response.data) //返回接口返回的錯誤信息
})

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM