一、背景需求
當頁面有大量數據需要渲染時,我們希望給用戶提供一個等待的預期
也就是說,在數據渲染之前,在頁面上顯示“正在加載”的簡易動畫
二、實現過程
我們可以把這一部分的代碼抽離出來,作為基礎/公用組件
// loading.vue
<template>
<div class="loading">
<img width="24" height="24" src="./loading.gif">
<p class="desc">{{this.title}}</p>
</div>
</template>
<script type="text/ecmascript-6">
export default {
name: 'Loading',
props: {
title: {
type: String,
default: '正在載入...'
}
}
}
</script>
<style scoped lang="stylus" rel="stylesheet/stylus">
@import "~common/stylus/variable"
.loading
width: 100%
text-align: center
.desc
line-height: 20px
font-size: $font-size-small
color: $color-text-l
</style>
然后在具體的頁面組件中,引入並注冊 Loading
用一個Div去包裹 Loading 標簽
並使用v-show來對這個Div的顯示和消失作切換
<div class="loading-container" v-show="!discList.length">
<Loading></Loading>
</div>
數組 discList 就是將要渲染的數據