vue自定义封装Loading组件

一、需求问题
在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,封装组件
<template>
    <div class="loader"></div>
</template>

<script>
export default {
   
    name: 'Loading'
}
</script>

<style lang="scss" scoped>
$colors:
  hsla(337, 84, 48, 0.75)
  hsla(160, 50, 48, 0.75)
  hsla(190, 61, 65, 0.75)
  hsla( 41, 82, 52, 0.75);
$size: 2.5em;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值