骨架屏加载

骨架屏

vue3的骨架屏使用开源包vue-content-loader

npm i vue-content-loader

npm地址:https://www.npmjs.com/package/vue-content-loader

骨架组件页面

<template>

  <content-loader></content-loader>
  
</template>

<script>
import { ContentLoader } from 'vue-content-loader'
export default {
    components: {
        ContentLoader
    }
}
</script>

<style>
</style>

内容页面

需要是一个异步组件 所以给setup添加async

<template>
  <div>
      <img :src="imgurl" alt="" class="ahufa">
  </div>
</template>

<script>
import bg from '../../public/img1.jpg'
//使用Promise模拟一个异步请求 1.5秒以后把图片地址传给imgurl
const getBgc = () => {
    return new Promise((resolve)=>{
        setTimeout(()=>{
            resolve(bg)
        },1500)
    })
}

export default {
	在正常情况下是不能给setup()添加async的 但Suspense要求为一个异步组件
    async setup(){
        const imgurl = await getBgc()
        return {imgurl}
    }
}
</script>

<style>
.ahufa {
    height: 600px;
}
</style>

App.vue

使用vue3新增的Suspense API

https://v3.cn.vuejs.org/guide/migration/suspense.html#%E4%BB%8B%E7%BB%8D

用法

<Suspense>
      <<template #default>
        <!-- 包含异步内容组件区域-->
      </template>
      
      <template #fallback>
        <!-- 骨架屏占位区域 -->
      </template>
</Suspense> 
<template>
  <div>
    <Suspense>
      <template #default>
        <!--包含异步内容组件区域-->
      <Asyncgay></Asyncgay>
      </template>

      <template #fallback>
        <!-- 骨架屏占位区域 -->
        <Loader></Loader>
      </template>
    </Suspense>  
  </div>
</template>

<script>
import Loader from './components/Loader.vue'
import Asyncgay from './components/Asyncgay.vue'
export default {
  name: 'App',
  components: {
    Loader,Asyncgay
  },
}
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值