一、需求问题
在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,封装组件
<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;
$thickness: 0.5em;
// Calculated variables.
$lat

最低0.47元/天 解锁文章
1345

被折叠的 条评论
为什么被折叠?



