場景
實際開發過程中,發送請求需要時間,在請求的數據未回來時,頁面會處于空白狀態 => 用戶體驗不好
需求
封裝一個 v-loading 指令,實現加載中的效果
分析
-
本質 loading效果就是一個蒙層,蓋在了盒子上
-
數據請求中,開啟loading狀態,添加蒙層
-
數據請求完畢,關閉loading狀態,移除蒙層
實現
1.準備一個 loading類,通過偽元素定位,設置寬高,實現蒙層
使用偽元素添加的好處就是添加和移除會方便一些,如果準備dom元素,則添加和移除需要把這個節點添加和移除。但如果是偽元素,只需要添加和移除類就行了。
2.開啟關閉 loading狀態(添加移除蒙層),本質只需要添加移除類即可
3.結合自定義指令的語法進行封裝復用
.loading:before {content: "";position: absolute;left: 0;top: 0;width: 100%;height: 100%;background: #fff url("./loading.gif") no-repeat center;
}
核心思路:
-
準備類名 loading,通過偽元素提供遮罩層
-
添加或移除類名,實現loading蒙層的添加移除
-
利用指令語法,封裝 v-loading 通用指令
inserted 鉤子中,binding.value 判斷指令的值,設置默認狀態
update 鉤子中,binding.value 判斷指令的值,更新類名狀態
代碼示例
<template><div class="main"><div class="box" v-loading="isLoading"><ul><li v-for="item in list" :key="item.id" class="news"><div class="left"><div class="title">{{ item.title }}</div><div class="info"><span>{{ item.source }}</span><span>{{ item.time }}</span></div></div><div class="right"><img :src="item.img" alt=""></div></li></ul></div><!-- 第二個盒子 --><div class="box2" v-loading="isLoading2"></div></div>
</template><script>
// 安裝axios => yarn add axios
import axios from 'axios'// 接口地址:http://hmajax.itheima.net/api/news
// 請求方式:get
export default {data () {return {list: [],isLoading: true,isLoading2: true}},async created () {// 1. 發送請求獲取數據const res = await axios.get('http://hmajax.itheima.net/api/news')setTimeout(() => {// 2. 更新到 list 中,用于頁面渲染 v-forthis.list = res.data.datathis.isLoading = false}, 2000)},directives: {loading: {inserted (el, binding) {// 應該需要根據isLoading的初始值關聯起來// el.classList.add('loading')binding.value ? el.classList.add('loading') : el.classList.remove('loading')},update (el, binding) {binding.value ? el.classList.add('loading') : el.classList.remove('loading')}}}
}
</script><style>
.loading:before {content: '';position: absolute;left: 0;top: 0;width: 100%;height: 100%;background: #fff url('./loading.gif') no-repeat center;
}.box2 {width: 400px;height: 400px;border: 2px solid #000;position: relative;
}.box {width: 800px;min-height: 500px;border: 3px solid orange;border-radius: 5px;position: relative;
}
.news {display: flex;height: 120px;width: 600px;margin: 0 auto;padding: 20px 0;cursor: pointer;
}
.news .left {flex: 1;display: flex;flex-direction: column;justify-content: space-between;padding-right: 10px;
}
.news .left .title {font-size: 20px;
}
.news .left .info {color: #999999;
}
.news .left .info span {margin-right: 20px;
}
.news .right {width: 160px;height: 120px;
}
.news .right img {width: 100%;height: 100%;object-fit: cover;
}
</style>