本文共 1336 字,大约阅读时间需要 4 分钟。
一、需求问题
在vue项目的开发中,会经常遇到这样的需求。当在页面内容进行加载的时候,会进行请求数据,然后显示页面。在这个等待的过程中,会出现一段时间的白屏,我们可以通过加一个loading的效果,进行过渡,然后显示页面。二、需求分析
components
文件夹中,建立Loading
文件夹,里面建立index.vue
文件,作为Loading
组件。这个加载组件在页面的多个位置中都可能会用的到,可以全局注册组件。在项目的main.js
文件中写以下的代码配置,进行全局注册。import Loading from '@/components/Loading'Vue.component('Loading', Loading);
在Loading
组件中,我们只需要写上相应的结构和样式,让其进行展示loading
的效果出来。
在需要使用Loading
组件的页面中,直接进行使用。通过组件内部使用 v-if
而使用isLoading
的值,在data
中设置isLoading
,默认为true
。当在数据请求完毕以后,再将 isLoading
的值设为false
。同时在使用Loading
组件下面的页面内容,需要使用v-else
,不然再请求完数据后无法显示页面内容。
三、需求实现
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')
Loading
下的index.vue
,封装组件
![]()
{ { item.nm }}
{ { item.wish }} 人想看
主演:{ { item.star }}
{ { item.rt }}上映
预售
转载地址:http://supg.baihongyu.com/