有一個需求:后端返回狀態碼-999表示token過期,前端要跳到登錄頁。我的代碼:http.js import axios from 'axios'
import Vue from "vue"
import VueRouter from 'vue-router'
import { Message } from "element-ui"; import common from './global.js'
Vue.use(VueRouter)
axios.interceptors.response.use( response => { //token過期處理
if(response.data.code == -999){
Message({
message: '身份已過期,請重新登錄!', type: 'warning'
}); this.$router.push('/login'); this.common.delCookie('token'); this.common.delLocal();
} return response;
}, error => {
Message({
message: '服務器響應失敗', type: 'warning'
}); return Promise.reject(error.response.data)
});提示了一句“Cannot read property '$router' of undefined”
添加回答
舉報
0/150
提交
取消