Implement global loading loading
To analyze the requirements, we only need to start loading when the request is initiated and close loading when the response is terminated. It's that simple, right?
import axios from 'axios'; import { Message, Loading } from 'element-ui'; import Cookies from 'js-cookie'; import router from '@/router/index' let loading //Define loading variable function startLoading() { //Use the Element loading-start method loading = ({ lock: true, text: 'loading……', background: 'rgba(0, 0, 0, 0.7)' }) } function endLoading() { //Use the Element loading-close method () } //Then showFullScreenLoading() tryHideFullScreenLoading() is to merge requests at the same time.//Declare a variable needLoadingRequestCount, and each time the showFullScreenLoading method is called needLoadingRequestCount + 1.//Call the tryHideFullScreenLoading() method, needLoadingRequestCount - 1. When needLoadingRequestCount is 0, loading ends.let needLoadingRequestCount = 0 export function showFullScreenLoading() { if (needLoadingRequestCount === 0) { startLoading() } needLoadingRequestCount++ } export function tryHideFullScreenLoading() { if (needLoadingRequestCount <= 0) return needLoadingRequestCount-- if (needLoadingRequestCount === 0) { endLoading() } } //http request interceptor( config => { var token = '' if(typeof ('user') === 'undefined'){ //It's empty at this time }else { token = (('user')).token }//Note that when using it, you need to introduce cookie methods. Recommend js-cookie = (); = { 'Content-Type':'application/json' } if(token != ''){ = token; } showFullScreenLoading() return config; }, error => { return (err); } ); //http response interceptor( response => { //Redirect to login page when the return information is not logged in or the login is invalid if( == 'W_100004' || == 'The user is not logged in or the login timeout, please log in! '){ ({ path:"/", querry:{redirect:}//Which page to jump from }) } tryHideFullScreenLoading() return response; }, error => { return (error) } )
The above example of vue+axios+element ui to implement global loading is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.