Vue3.0 手写加载更多组件

1.加载更多封装成全局组件,步骤跟我封装全局轮播图,全局骨架屏一样,可以看一下

这里用到了vueuse,先装包 npm i  @vueuse/core

这里,你可以直接复制使用: 

<template>
  <div class="xtx-infinite-loading" ref="target">
    <!-- 正在加载数据时显示 -->
    <div class="loading" v-if="isLoading">
      <span class="img"></span>
      <span class="text">正在加载...</span>
    </div>
    <!-- 数据全部加载完毕时显示 -->
    <div class="none" v-if="isFinished">
      <span class="text">亲,没有更多了</span>
    </div>
  </div>
</template>
<script>
import { ref } from 'vue'
import { useIntersectionObserver } from '@vueuse/core'
export default {
  name: 'XtxInfiniteLoading',
  props: {
    isLoading: { type: Boolean, default: true }, // 是否正在加载
    isFinished: { type: Boolean, default: false } // 有没有全部加载完成
  },
  setup (props, { emit }) {
    const target = ref(null)
    useIntersectionObserver(target,
      ([{ isIntersecting }], dom) => {
        if (isIntersecting && props.isLoading === false && props.isFinished === false) {
          emit('load')
        }
        console.log('是否可见', isIntersecting)
      })

    return { target }
  }
}
</script>

<style scoped lang='less'>
.xtx-infinite-loading {
  .loading {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200px;
    .img {
      width: 50px;
      height: 50px;
      background: url('~@/assets/images/load.gif') no-repeat center / contain;
    }
    .text {
      color: #999;
      font-size: 16px;
    }
  }
  .none {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200px;
    .text {
      color: #999;
      font-size: 16px;
    }
  }
}
</style>

2.父组件需要传给子组件: 1.是否加载完成 2.是否加载中....

上面子组件判断,如果进入可视区域,并且也不是加载中....,也没有加载完成,这时候给父组件抛出事件。父组件需要把是否加载状态改为加载中,开始发请求,请求回来的数据如果你直接赋值给goodsList的话,假如说一页是20条,那你显示的就只是20条(每页的最新20条数据),所以你需要把请求回来的每页数据 push 到 goodsList中。load是子组件传递给父组件的事件

以下是写在父组件中的代码:

 <XtxInfinteLoading  :isFinished="isFinished" :isLoading="isLoading" @load="loadData"/>
 const loadData = () => {
      isLoading.value = true
      findSubCategoryGoods(reqParams).then(data => {
        console.log('findSubCategoryGoods', data)
        // 新数据要追加到数组中
        goodsList.value.push(...data.result.items)
        reqParams.page++  // 页码++,重新请求下一页数据
        // 判断是否加载完成
        if (data.result.items.length === 0) {
          isFinished.value = true
        }
        isLoading.value = false
      })
    }

最开始reqParams.page=1,求得第一页的数据,然后页码+1,此时 reqParams.page=2,带着2去请求第二页的数据,以此类推。。。。直到取不到数据,如果取不到数据,isFinish=true,子组件中,没有更多了字样显示

  let reqParams = {
      page: 1,
      pageSize: 20,
      categoryId: router.params.id,
      sortField: null, // 排序类别
      attrs: [], // 商品属性
      brandId: null // 品牌名称
    }

注册全局的时候,如果你写的 .name

 这名字要一致

 要么就app.component('xxxxx' ,xxxxx )

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值