创建一个axios.js并引入相关的包
nprogress是http请求时的加载条样式,必须同时引入nprogress和nprogress.css
在开始设置一个网络请求超时的操作,10000ms后重新加载一次http请求
axios.defaults.timeout = 10000;
设置拦截器
request拦截器
axios.interceptors.request.use(config => {// Do something before request is sentNProgress.start()return config;},error => {// Do something with request errorreturn Promise.reject(error);});
response拦截器
axios.interceptors.response.use(response => {// Do something before response is sentNProgress.done()return response;},error => {// Do something with response errorreturn Promise.reject(error);});
此时页面发送http请求时就会出现加载条样式!