博客
关于我
vue自定义封装Loading组件
阅读量:365 次
发布时间: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/

你可能感兴趣的文章
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>