国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

基于vue+element實現全局loading過程詳解

瀏覽:7日期:2023-01-08 16:05:59

項目中使用的是vue+element實現的全局loading

1.引入所需組件,這里主要就是router和element組件,element組件引入可以參考element官網

2.下面就是重點及代碼實現了

首先是全局的一個變量配置參數,代碼如下:

//全局頁面跳轉是否啟用loadingexport const routerLoading = true;//全局api接口調用是否啟用loadingexport const apiLoading = true;//loading參數配置export const loadingConfig = { lock: true, text: ’Loading’, spinner: ’el-icon-loading’, background: ’rgba(0, 0, 0, 0.7)’}

接下來是全局的一個loading簡單封裝,代碼如下

import ElementUI from ’element-ui’;import {loadingConfig} from ’../src/config/index’ //全局的一個基本參數配置var loading = null ;const loadingShow = () => { loading = ElementUI.Loading.service(loadingConfig);}const loadingHide = () => { loading.close();}const loadingObj={ loadingShow, loadingHide}export default loadingObj

頁面跳轉時全局配置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組件的簡單封裝import {routerLoading} from ’../src/config/index’ //全局的頁面跳轉loading是否啟用Vue.use(ElementUI);Vue.config.productionTip = false/* eslint-disable no-new */new Vue({ el: ’#app’, router, components: { App }, template: ’<App/>’})//從后臺獲取的用戶角色const role = ’user’//當進入一個頁面是會觸發導航守衛 router.beforeEach 事件router.beforeEach((to,from,next) => { routerLoading ? glo_loading.loadingShow() : ’’ //如果全局啟用頁面跳轉則加載loading if(to.meta.roles){ if(to.meta.roles.includes(role)){ next() //放行 }else{ next({path:'/404'}) //跳到404頁面 } }else{ next() //放行 }routerLoading ? glo_loading.loadingHide() : ’’//關閉loading層})

在ajax請求的時候調用全局loading,代碼如下:

// 添加請求攔截器service.interceptors.request.use(function (config) { // 在發送請求之前做些什么 apiLoading ? glo_loading.loadingShow() : ’’//全局loading是否啟用 return config;}, function (error) { // 對請求錯誤做些什么 console.log(error); return Promise.reject(error);});// 添加響應攔截器service.interceptors.response.use(function (response) { // 對響應數據做點什么 if(response.status == 200){ return response.data; }else{ Promise.reject(); }}, function (error) { // 對響應錯誤做點什么 console.log(error); apiLoading ? glo_loading.loadingHide() : ’’ return Promise.reject(error);});

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Vue
相關文章:
主站蜘蛛池模板: 尼木县| 屯留县| 罗城| 万山特区| 绥江县| 宽甸| 武川县| 西丰县| 凤阳县| 闻喜县| 封丘县| 普兰店市| 沂水县| 博野县| 贺兰县| 揭东县| 孟村| 叶城县| 格尔木市| 娄烦县| 鸡泽县| 商河县| 贵港市| 恩平市| 大埔县| 孟州市| 固安县| 安顺市| 错那县| 微山县| 瓦房店市| 江安县| 资中县| 松潘县| 禄丰县| 胶南市| 奉化市| 闸北区| 水城县| 水富县| 鹤壁市|