element ui loading加載開啟與關(guān)閉方式
element ui loading加載開啟與關(guān)閉
我們?cè)谑褂胑lement ui loading加載組件的服務(wù)方式的時(shí)候想要在請(qǐng)求開始之前開啟組件,請(qǐng)求成功或者失敗時(shí)關(guān)閉組件,官網(wǎng)上是在一個(gè)方法里面寫著開啟與關(guān)閉,所以我們可以做一個(gè)小小的封裝;
//Loading加載 openFullScreen() { const loading = this.$loading({ lock: true, text: 'Loading', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.7)' }); return loading; }
這里我們?cè)陂_啟loading的同時(shí),返回loading;
closeFullScreen(loading){ loading.close(); },
關(guān)閉時(shí)接受開啟返回的loading;
//開啟 this.openFullScreen(); //vue框架 axios請(qǐng)求 this.$http.post("url").then(res => { //請(qǐng)求成功關(guān)閉; this.closeFullScreen(this.openFullScreen()); }).catch(()=>{ //請(qǐng)求失敗關(guān)閉; this.closeFullScreen(this.openFullScreen()); })
封裝完的使用;
使用element ui的Loading組件
Element-UI 提供了 Loading 組件,通過對(duì)于這個(gè)組件進(jìn)行一些處理,我們能做到在發(fā)送請(qǐng)求的時(shí)候進(jìn)行 loading 操作
實(shí)現(xiàn)方式
先寫一個(gè)loading.js文件
import { Loading } from 'element-ui'; let loadingCount = 0; let loading; const startLoading = () => { loading = Loading.service({ lock: true, text: '加載中……', background: 'rgba(0, 0, 0, 0.7)' }); }; const endLoading = () => { loading.close(); }; export const showLoading = () => { if (loadingCount === 0) { startLoading(); } loadingCount += 1; }; export const hideLoading = () => { if (loadingCount <= 0) { return; } loadingCount -= 1; if (loadingCount === 0) { endLoading(); } };
再在axios的interceptor中調(diào)用
// ... import { showLoading, hideLoading } from './loading'; /* 請(qǐng)求攔截器(請(qǐng)求之前的操作) */ ajax.interceptors.request.use((req) => { showLoading(); return req; }, err => Promise.reject(err)); /* 請(qǐng)求之后的操作 */ ajax.interceptors.response.use((res) => { hideLoading(); return res; return Promise.reject(res); }, (err) => { hideLoading(); return Promise.reject(err); });
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue子組件向父組件通信與父組件調(diào)用子組件中的方法
這篇文章主要介紹了Vue子組件向父組件通信與父組件調(diào)用子組件中的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06vue3.0?router路由跳轉(zhuǎn)傳參問題(router.push)
這篇文章主要介紹了vue3.0?router路由跳轉(zhuǎn)傳參問題(router.push),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02VUE v-model表單數(shù)據(jù)雙向綁定完整示例
這篇文章主要介紹了VUE v-model表單數(shù)據(jù)雙向綁定,結(jié)合完整實(shí)例形式分析了vue.js實(shí)現(xiàn)表單數(shù)據(jù)雙向綁定相關(guān)操作技巧,需要的朋友可以參考下2019-01-01Vue 針對(duì)瀏覽器參數(shù)過長實(shí)現(xiàn)瀏覽器參數(shù)加密解密的操作方法
文章介紹了如何在Vue項(xiàng)目中使用crypto-js庫對(duì)瀏覽器參數(shù)進(jìn)行加密和解密,以解決參數(shù)過長的問題,在router/index.js中添加了相關(guān)代碼,并在utils工具類中添加了encryption.js和query.js源碼,感興趣的朋友一起看看吧2024-12-12vue 數(shù)組和對(duì)象不能直接賦值情況和解決方法(推薦)
這篇文章主要介紹了vue 數(shù)組和對(duì)象不能直接賦值情況和解決方法,需要的朋友可以參考下2017-10-10