vue3 骨架屏+上拉加载更多封装

vue3 骨架屏+上拉加载更多封装

  • 介绍
    因业务需求要用到滚动加载,就用到了vant组件库里的List组件,然后突发奇想封装了个骨架屏进去,api和使用方法还有文档都参考了vant的list组件,不过没有参考源码,此组件是我第一次封装,比较贴合自身业务,所以不太符合需求的同学可以直接修改源码使用,感谢vant团队!vant的List组件地址 (https://vant-contrib.gitee.io/vant/v3/#/zh-CN/list)

    提供骨架屏展示、瀑布流滚动加载,用于展示长列表,当列表即将滚动到底部时,会触发事件并加载更多列表项。

  • 效果
    在这里插入图片描述

  • 引入

	import ListView from '@/components/list_view/list_view.vue'
	components:{
   
        ListView
    }
  • 代码演示
    – 用法
<div class="router_view" style="height:800px;overflow-y: auto;">
	<ListView 
        :list-data="data"  
        :bind-scroll-document="routerView" 
        :empty-item="emptyItem" 
        :finished="finished"
        v-model:loading="showLoading"
        v-model:error="showError"
        @load="requestData"
        v-slot="{ item }"
        >
          <div class="item row-center item-between">
                <img
                    class="item_pic"
                    :src="item.full_photo"
                />
                <div class
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
对于Vue 3和TypeScript结合使用Element Plus进行懒加载指令的封装,可以按照以下步骤进行: 1. 创建一个新的文件,例如`lazyLoadDirective.ts`,在其中导入Vue、Directive和IntersectionObserver。 ```typescript import { App, Directive, DirectiveBinding } from 'vue'; import { DirectiveOptions } from 'vue'; // 导入IntersectionObserver import IntersectionObserver from 'intersection-observer'; // 创建指令选项对象 const lazyLoadDirective: DirectiveOptions = { // 指令生命周期钩子函数 mounted(el: HTMLElement, binding: DirectiveBinding) { // 创建IntersectionObserver实例 const observer = new IntersectionObserver((entries) => { // 当目标元素进入可视区域时加载内容 if (entries[0].isIntersecting) { // 执行绑定的回调函数 binding.value(); // 关闭观察器 observer.disconnect(); } }); // 开始观察目标元素 observer.observe(el); } }; // 导出指令对象 export default lazyLoadDirective; ``` 2. 在`main.ts`文件中导入并注册该指令。 ```typescript import { createApp } from 'vue'; import App from './App.vue'; import lazyLoadDirective from './lazyLoadDirective.ts'; const app = createApp(App); // 注册指令 app.directive('lazyload', lazyLoadDirective); app.mount('#app'); ``` 3. 在组件中使用指令。 ```html <template> <div v-lazyload="loadContent">Lazy Load Content</div> </template> <script> export default { methods: { loadContent() { // 加载内容的逻辑 } } } </script> ``` 以上是一种简单的方式来封装加载指令。请注意,此代码仅供参考,你可能需要根据具体的需求进行适当的修改和调整。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值