vant在拦截器中全局添加loading

接口请求时候加载loading,这里使用的vue+vant

import axios from 'axios';
import { Toast } from 'vant';
const instance = axios.create({
    baseURL: 'xxx',
    timeout: 100000,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
    }
});
 
let loading       
function startLoading() {  
    loading = Toast.loading({
        message: '加载中...',
        forbidClick: true,
    })
}
 
function endLoading() {   
    loading.clear()
}
// 添加请求拦截器
instance.interceptors.request.use(config => {
    startLoading();
    return config
}, error => {
    return Promise.reject(error)
});
 
//响应拦截器
instance.interceptors.response.use(response => {
    endLoading();
    return response.data
})
export default instance

 

 

posted @ 2022-08-11 10:39  Magi黄元  阅读(1740)  评论(0编辑  收藏  举报