uniapp3.0列表公用hooks封装,包含上拉加载下一页,下拉刷新,移动端分页公用hook

uniapp3.0列表公用hooks封装,包含上拉加载下一页,下拉刷新,接口动态化,无数据提示

hook

usePaginatedList.js

// 列表hooks
export const usePaginatedList = (fetchFunction, initialQuery) => {
  const list = ref([])
  const pageNo = ref(1)
  const pageSize = ref(initialQuery.pageSize || 10)
  const noMoreData = ref(false)
  const loading = ref(false)
  const status = ref('more')
  status.value = 'loading'
  const fetchList = (isRefresh = false) => {
    uni.showLoading() // 加载
    if (isRefresh) {
      pageNo.value = 1
      list.value = []
      noMoreData.value = false
      status.value = 'loading'
    } else {
      status.value = 'loading'
    }

    loading.value = true
    let query = {
      ...initialQuery,
      'pageNo': pageNo.value,
      'pageSize': pageSize.value,
    }
    fetchFunction(query).then(res => {
      uni.hideLoading()
      uni.stopPullDownRefresh() // 停止下拉刷新
      loading.value = false
      if (res && res.code == 0) {
        if (res.data.records.length < pageSize.value) {
          noMoreData.value = true
          status.value = 'noMore'
        } else {
          status.value = 'more'
        }
        if (isRefresh) {
          list.value = res.data.records
        } else {
          list.value = [...list.value, ...res.data.records]
        }
        pageNo.value++
      } else {
        tip.error(res.msg)
        status.value = 'more'
      }
    }).catch(() => {
      loading.value = false
      uni.hideLoading()
      uni.stopPullDownRefresh() // 停止下拉刷新
      tip.error('数据获取失败')
      status.value = 'more'
    })
  }

  const ReachBottom = () => {
    if (!noMoreData.value && !loading.value) {
      fetchList()
    }
  }

  const onPullDown = () => {
    fetchList(true)
  }

  return {
    list,
    loading,
    noMoreData,
    fetchList,
    ReachBottom,
    onPullDown,
    status
  }
}

页面使用
 

<template>
  <view class="container">
    <view class="padding">
      <scroll-view scroll-y class="scroll-box">
        <view class="cardBoxmain padding margin-bottom " v-for="(item,index) in list" :key="index">
         //列表内容.......
        </view>
          //此处自封的空白提示,可自行替换
        <empty v-if="!list.length"></empty>
        <uni-load-more :status="status" v-else/>
      </scroll-view>
    </view>
  </view>
</template>

<script setup>
  import api from "@/api/car/car.js"
  import {
    usePaginatedList
  } from '@/hooks/usePaginatedList'
  onShow(() => {
    fetchList()
  });

  // 定义获取数据的方法
  const fetchFunction = (query) => {
    return api.queryChargeHis(query)
  }
  const initialQuery = {
    'pageSize': 10,
    'resType': '7'//其他参数自己定义
  }

  const {
    list,
    fetchList,
    ReachBottom,
    onPullDown,
    status
  } = usePaginatedList(fetchFunction, initialQuery)

  // 监听页面上拉触底事件
  onReachBottom(() => {
    console.log('触底啦')
    ReachBottom()
  })

  // 监听页面下拉刷新事件
  onPullDownRefresh(() => {
    console.log('下拉刷新啦')
    onPullDown()
  })

 
</script>

<style scoped lang="scss">
  
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 React Hooks 中返回上一页并不会刷新上一页的页面,这是 react-router-dom 库的默认行为。如果你需要在返回上一页时刷新上一页的页面,可以通过以下几种方式实现: 1. 在返回上一页时手动刷新页面。可以使用 JavaScript 提供的 `location.reload()` 方法来刷新页面。具体来说,可以在需要返回上一页的事件处理函数中先调用 `history.goBack()` 方法,然后再调用 `location.reload()` 方法来刷新页面。 ```jsx import { useHistory } from 'react-router-dom'; function MyComponent() { const history = useHistory(); function handleGoBack() { history.goBack(); window.location.reload(); } return ( <div> <button onClick={handleGoBack}>返回上一页</button> </div> ); } ``` 2. 使用路由参数来判断是否需要刷新页面。可以在路由跳转时传递一个参数,然后在上一页的组件中监听该参数的变化,如果参数发生了变化,则手动刷新页面。具体来说,可以在需要返回上一页的组件中通过 `history.push()` 方法传递一个参数,然后在上一页的组件中通过 `useEffect()` 钩子函数监听该参数的变化,如果参数发生了变化,则调用 `location.reload()` 方法来刷新页面。 ```jsx import { useEffect } from 'react'; import { useHistory } from 'react-router-dom'; function PreviousComponent() { useEffect(() => { if (location.state && location.state.forceRefresh) { window.location.reload(); } }, []); return ( <div> <h1>上一页</h1> </div> ); } function MyComponent() { const history = useHistory(); function handleGoBack() { history.push({ pathname: '/previous-page', state: { forceRefresh: true } }); } return ( <div> <button onClick={handleGoBack}>返回上一页</button> </div> ); } ``` 需要注意的是,手动刷新页面可能会导致用户的操作数据丢失,因此需要谨慎使用。建议在需要刷新页面时给出提示并允许用户确认。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值