问题描述
在this.$router发现没这个属性,后面查了下 this为undefined
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
import axios from 'axios'
import {Loading, Message} from 'element-ui'
import Debounce from 'lodash/debounce'
const Axios = axios.create({
baseURL:'/',
timeout:'10000',
responseType:'json',
withCredentials:true,
headers:{
"Content-Type":"application/json; charset=UTF-8"
}
});
let needLoadingRequestCount = 0;
let loadingObj;
Axios.interceptors.request.use(
config=>{
console.log(this);
if(config.showLoading){
showFullScreenLoading();
}
return config;
},
error=>{
if(needLoadingRequestCount !== 0){
loadingObj.close();
}
Message({
showClose:true,
message:'服务器有误!请稍后重试',
type:"error"
});
return Promise.reject(error);
}
);
Axios.interceptors.response.use(
res =>{
// console.log(this);
if(res.config.showLoading){
tryHideFullScreenLoading();
}
if(res.data.code !== 200 && res.data.message){
Message({
showClose:false,
type:"error",
center:true,
message:res.data.message ? res.data.message : "服务器繁忙! 请重试"
});
if(res.data.code === 9999100){
this.$router.push('/login');
}
return Promise.reject(res.data.message);
}
return res;
},
error =>{
if(needLoadingRequestCount !== 0){
loadingObj.close();
}
Message({
showClose:true,
message:'服务器繁忙!请稍后重试',
type:"error"
});
return Promise.reject(error);
}
);
function showFullScreenLoading(){
if(needLoadingRequestCount === 0){
loadingObj = Loading.service({
lock:true,
text:"Loading",
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)'
});
}
needLoadingRequestCount ++
}
function tryHideFullScreenLoading(){
if(needLoadingRequestCount <= 0) return
needLoadingRequestCount --
if(needLoadingRequestCount === 0){
Debounce(tryCloseLoading,300)();
}
}
function tryCloseLoading(){
if(needLoadingRequestCount === 0){
loadingObj.close();
}
}
export default {
header(token){
Axios.defaults.headers.common['Authorization'] = `ticket ${token}`;
},
post:(url,data,config = {showLoading : true}) => Axios.post(url,data,config),
get:(url,config = {showLoading : true}) => Axios.get(url,config),
delete:(url,config = {showLoading : true}) => Axios.delete(url,config)
}
你期待的结果是什么?实际看到的错误信息又是什么?
明明都是箭头函数啊,this怎么会丢呢
Vue实例还未生成b啊,
import Vue from 'vue'
Vue.$router
or
import router from '../router.js'(路由的路径)
然后router.push...