基于vue+element實(shí)現(xiàn)全局loading過(guò)程詳解
項(xiàng)目中使用的是vue+element實(shí)現(xiàn)的全局loading
1.引入所需組件,這里主要就是router和element組件,element組件引入可以參考element官網(wǎng)
2.下面就是重點(diǎn)及代碼實(shí)現(xiàn)了
首先是全局的一個(gè)變量配置參數(shù),代碼如下:
//全局頁(yè)面跳轉(zhuǎn)是否啟用loadingexport const routerLoading = true;//全局api接口調(diào)用是否啟用loadingexport const apiLoading = true;//loading參數(shù)配置export const loadingConfig = { lock: true, text: ’Loading’, spinner: ’el-icon-loading’, background: ’rgba(0, 0, 0, 0.7)’}
接下來(lái)是全局的一個(gè)loading簡(jiǎn)單封裝,代碼如下
import ElementUI from ’element-ui’;import {loadingConfig} from ’../src/config/index’ //全局的一個(gè)基本參數(shù)配置var loading = null ;const loadingShow = () => { loading = ElementUI.Loading.service(loadingConfig);}const loadingHide = () => { loading.close();}const loadingObj={ loadingShow, loadingHide}export default loadingObj
頁(yè)面跳轉(zhuǎn)時(shí)全局配置loading,代碼如下:
main.js中添加代碼:
// The Vue build version to load with the `import` command// (runtime-only or standalone) has been set in webpack.base.conf with an alias.import Vue from ’vue’import App from ’./App’import router from ’./router’import ElementUI from ’element-ui’;import ’element-ui/lib/theme-chalk/index.css’;import glo_loading from ’../loading/index’ //loading組件的簡(jiǎn)單封裝import {routerLoading} from ’../src/config/index’ //全局的頁(yè)面跳轉(zhuǎn)loading是否啟用Vue.use(ElementUI);Vue.config.productionTip = false/* eslint-disable no-new */new Vue({ el: ’#app’, router, components: { App }, template: ’<App/>’})//從后臺(tái)獲取的用戶角色const role = ’user’//當(dāng)進(jìn)入一個(gè)頁(yè)面是會(huì)觸發(fā)導(dǎo)航守衛(wèi) router.beforeEach 事件router.beforeEach((to,from,next) => { routerLoading ? glo_loading.loadingShow() : ’’ //如果全局啟用頁(yè)面跳轉(zhuǎn)則加載loading if(to.meta.roles){ if(to.meta.roles.includes(role)){ next() //放行 }else{ next({path:'/404'}) //跳到404頁(yè)面 } }else{ next() //放行 }routerLoading ? glo_loading.loadingHide() : ’’//關(guān)閉loading層})
在ajax請(qǐng)求的時(shí)候調(diào)用全局loading,代碼如下:
// 添加請(qǐng)求攔截器service.interceptors.request.use(function (config) { // 在發(fā)送請(qǐng)求之前做些什么 apiLoading ? glo_loading.loadingShow() : ’’//全局loading是否啟用 return config;}, function (error) { // 對(duì)請(qǐng)求錯(cuò)誤做些什么 console.log(error); return Promise.reject(error);});// 添加響應(yīng)攔截器service.interceptors.response.use(function (response) { // 對(duì)響應(yīng)數(shù)據(jù)做點(diǎn)什么 if(response.status == 200){ return response.data; }else{ Promise.reject(); }}, function (error) { // 對(duì)響應(yīng)錯(cuò)誤做點(diǎn)什么 console.log(error); apiLoading ? glo_loading.loadingHide() : ’’ return Promise.reject(error);});
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)2. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車(chē)輛管理系統(tǒng)3. HTML5實(shí)戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)4. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫(xiě)金額)的函數(shù)5. Jsp servlet驗(yàn)證碼工具類(lèi)分享6. jscript與vbscript 操作XML元素屬性的代碼7. 基于PHP做個(gè)圖片防盜鏈8. Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件列表展示(二)9. asp.net core 認(rèn)證和授權(quán)實(shí)例詳解10. XML在語(yǔ)音合成中的應(yīng)用
