Loading 加载 Taro + vue3 自定义组件的封装和 分页 优化

1.需求

  当需要实现一个组件 上拉加载的组件 我们可以选择某些组件库的组件。

  但是有的组件没有这个组件,比如跟Taro 框架配套的京东nut-ui组件库 没有提供这个功能,

2.Loading组件

①封装

<template>
    <div class="container">
        
        <div class="tip" v-if="page >= total && tipFlag">没有更多数据了呢~</div>
        <div class="loading-box" v-if="loadingFlag && page <= total">
            <div class="loading-box-text">

                <div class="loading"></div>
                <div class="text">正在加载中...</div>
            </div>
        </div>
        <div style="height: 50px;"></div>

    </div>
</template>
<script setup>
import { ref, toRefs } from "vue";
const props = defineProps({
    page: Number, //接受页数
    total: Number, //接收总页数
    loadingFlag: Boolean, //是否正在加载数据
    tipFlag: Boolean, //是否显示 "没有更多数据的提示"
});
const { pag, total, loadingFlag, tipFlag } = toRefs(props)
</script>
<style lang="scss">
.container {
    padding: 30px;
    .tip {
        color: #858a99;
        font-size: 24px;
        text-align: center;
        margin: 5px;
        
    }

    .loading-box {
        display: flex;
        justify-content: center;
        align-items: center;
        margin-bottom: 6px;

        .loading-box-text {
            display: flex;
            align-items: center;
            color: #858a99;

            .text {
                font-size: 18px;
                margin-left: 8px;
            }

            .loading {
                width: 14px;
                height: 14px;
                border: 2px solid #858a99;
                border-top-color: transparent;
                border-radius: 100%;
                text-align: center;
                animation: circle infinite 0.75s linear;
            }

            // 转转转动画
            @keyframes circle {
                0% {
                    transform: rotate(0);
                }

                100% {
                    transform: rotate(360deg);
                }
            }
        }


    }
}
</style>

② 使用 

   <Loading :page="pageinfo.currentPage" :total="totalpage" :loadingFlag="loadingFlag"
                            :tipFlag="tipFlag">
                        </Loading>

3.分页节流的使用

//分页 节流
const throttle = (func, delay) => {
    let lastTime = 0;
    return function () {
        const now = new Date().getTime();
        if (now - lastTime >= delay) {
            func.apply(this, arguments);
            lastTime = now;
        }
    };
}
//分页
const onScrollBottom = throttle(() => {
    console.log("到底了");
    loadingFlag.value = true;
    tipFlag.value = false;

    if (pageInfo.value.currentPage < total.value) {
        pageInfo.value.currentPage++;
        getRuleList(1);
    } else {
        loadingFlag.value = false;
        tipFlag.value = true;
    }
}, 200);

// 在绑定滚动事件时使用 onScrollBottom
window.addEventListener('scroll', onScrollBottom);//获取规则列表

相关推荐

  1. 【uniapp】Vue3移动端滚动 组件封装

    2024-01-01 00:32:03       15 阅读
  2. Vue3图片懒封装定义指令

    2024-01-01 00:32:03       11 阅读
  3. Vue3优化之实现懒组件异步

    2024-01-01 00:32:03       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-01 00:32:03       17 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-01 00:32:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-01 00:32:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-01 00:32:03       18 阅读

热门阅读

  1. GBASE南大通用-CodeFirst 模式

    2024-01-01 00:32:03       39 阅读
  2. 关于解决el-select组件自动清除数据空格的问题

    2024-01-01 00:32:03       33 阅读
  3. Linux: eBPF: bcc-tools:tcpdrop使用需要注意的问题

    2024-01-01 00:32:03       39 阅读
  4. 探索抖音详情API:塑造未来的媒体交互

    2024-01-01 00:32:03       40 阅读
  5. 第二百三十一回

    2024-01-01 00:32:03       39 阅读
  6. CSS实用功能

    2024-01-01 00:32:03       42 阅读
  7. Ubuntu20.04 防火墙配置

    2024-01-01 00:32:03       34 阅读
  8. 蓝牙技术在物联网中的应用

    2024-01-01 00:32:03       37 阅读