Vue實現(xiàn)骨架屏的示例代碼
vue實現(xiàn)頁面加載占位
效果如下
思路與實現(xiàn)
頁面加載時用戶等待,此時與用戶交互的方式通常有等待層、進度條、加載動畫等;本次介紹加載占位,把頁面即將展示的樣子用css背景展示出來,等數(shù)據(jù)返回后即可進行頁面渲染,可以有效減少頁面抖動。
頁面組成如下:
占位子組件:使用純css編寫;
獲取數(shù)據(jù)的交易:此處使用setTimeout模擬查詢耗時;
數(shù)據(jù)展示組件;
代碼如下:
<template> <div> <title-bar :title="title" @goBack="goback"></title-bar> <div v-if="res==true"> <div v-for="(prd, index) in productList" :key="index"> <prd-item :prd="prd" @toPrdDetail="toPrdDetail"></prd-item> </div> </div> <list-loading v-else></list-loading> </div> </template> <script> import TitleBar from "@/components/TitleBar"; import ListLoading from '@/components/ListLoading'; import PrdItem from "./components/PrdItem"; export default { name: "hgang", // 分割線 components: { ListLoading, TitleBar, PrdItem }, data() { return { res: false, // 數(shù)據(jù)是否已經(jīng)返回 title: '產(chǎn)品列表', productList: [ { imgPath: "apple-1001.png", name: "Apple iPad Air 平板電腦(2020新款)", price: "4799.00", sale: "5", ranking: "10000+評價 平板熱賣第5名", prdShopName: "Apple官方旗艦店" } ] }; }, mounted() { console.log(111); this.waitDateload(); }, methods: { waitDateload() { console.log(this.res); setTimeout(() => { this.res = true; console.log(this.res); }, 5000); }, toPrdDetail() { // }, goback() { // } }, }; </script>
其中:
- ListLoading:加載占位子組件,使用css編寫,另加閃光動畫效果;
- PrdItem:數(shù)據(jù)返回之后頁面渲染子組件;
- res:控制占位組件與實際頁面切換變量,通過v-if來控制頁面展示,數(shù)據(jù)返回立刻重新渲染頁面。
到此這篇關于Vue實現(xiàn)骨架屏的示例代碼的文章就介紹到這了,更多相關Vue骨架屏內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Vue3中shallowRef和shallowReactive的使用
這篇文章主要為大家介紹了Vue3中shallowRef和shallowReactive函數(shù)的使用方法,文中的示例代碼講解詳細,對我們學習Vue有一定的幫助,需要的可以參考一下2022-07-07vuejs element table 表格添加行,修改,單獨刪除行,批量刪除行操作
這篇文章主要介紹了vuejs element table 表格添加行,修改,單獨刪除行,批量刪除行操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07