使用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) //返回接口返回的錯誤信息 })