博客
关于我
vue自定义封装Loading组件
阅读量:357 次
发布时间:2019-03-05

本文共 1336 字,大约阅读时间需要 4 分钟。

一、需求问题

在vue项目的开发中,会经常遇到这样的需求。当在页面内容进行加载的时候,会进行请求数据,然后显示页面。在这个等待的过程中,会出现一段时间的白屏,我们可以通过加一个loading的效果,进行过渡,然后显示页面。

二、需求分析

  1. components文件夹中,建立Loading文件夹,里面建立index.vue文件,作为Loading组件。这个加载组件在页面的多个位置中都可能会用的到,可以全局注册组件。在项目的main.js文件中写以下的代码配置,进行全局注册。
import Loading from '@/components/Loading'Vue.component('Loading', Loading);
  1. Loading组件中,我们只需要写上相应的结构和样式,让其进行展示loading的效果出来。

  2. 在需要使用Loading组件的页面中,直接进行使用。通过组件内部使用 v-if 而使用isLoading 的值,在data中设置isLoading,默认为true。当在数据请求完毕以后,再将 isLoading的值设为false。同时在使用Loading组件下面的页面内容,需要使用v-else,不然再请求完数据后无法显示页面内容。

三、需求实现

  1. main.js
import Vue from 'vue'import App from './App.vue'import router from './router'import store from './store'import axios from 'axios'Vue.prototype.axios = axios;Vue.filter('setWH', (url, arg) => {     return url.replace(/w\.h/, arg);});Vue.config.productionTip = false;import Scroller from '@/components/Scroller'//  全局注册 Scroller 组件Vue.component('Scroller', Scroller);import Loading from '@/components/Loading'Vue.component('Loading', Loading);new Vue({     router,  store,  render: h => h(App)}).$mount('#app')
  1. Loading下的index.vue,封装组件
  1. 使用组件

转载地址:http://supg.baihongyu.com/

你可能感兴趣的文章
python爬虫——代理IP
查看>>
二、bootstrap4基础(flex布局)
查看>>
三、案例:留言板 & url.parse()
查看>>
Python中的filter()函数!!!1
查看>>
(新手小白必学!)用Python设计和实现聪明的尼姆游戏(人机对战)!!!!
查看>>
LeetCode:283. 移动零!!!1
查看>>
Python实验26:计算文件MD5值
查看>>
端口探测
查看>>
LeetCode:28. 实现 strStr()——————简单
查看>>
java 中 private default protected public 范围
查看>>
LeetCode:697. 数组的度————简单
查看>>
LeetCode:1052. 爱生气的书店老板————中等
查看>>
C语言的6大基本数据类型!(学习C语言小白必备!!)
查看>>
红黑树学习
查看>>
vue中导入导入 Mint-UI的注意事项
查看>>
Vue——mock模拟数据的使用
查看>>
Nginx配置反向代理与负载均衡
查看>>
高阶函数reduce
查看>>
Lionheart万汇:布林线双底形态分析技巧
查看>>
Lionheart万汇:台积电大幅提升资本开支,2021有望续创辉煌
查看>>