小程序-滚动触底-页面列表数据无限加载

// index/index.vue

<template>

<!-- 自定义导航栏 -->

<CustomNavbar />

<scroll-view @scrolltolower="onScrolltolower" scroll-y class="scroll-view">

<!-- 猜你喜欢 -->

<Guess ref="guessRef" />

</scroll-view>

</template>

<script>

// 滚动触底事件

const guessRef = ref<GuessInstance>()

const onScrolltolower = () => {

console.log('滚动到底部啦-guessRef', guessRef.value)

guessRef.value?.getMore()

}

</script>

// src/index/components/guess.vue

<script setup lang="ts">

import { getHomeGoodsGuessLikeAPI } from '@/services/home'

import type { PageParams } from '@/types/global'

import type { GuessItem } from '@/types/home'

import { onMounted, ref } from 'vue'

// 分页参数

const pageParams: Required<PageParams> = {

page: 1,

pageSize: 10,

}

// 猜你喜欢的列表

const guessList = ref<GuessItem[]>([])

// 已结束标记

const finish = ref(false)

// 获取猜你喜欢数据

const getHomeGoodsGuessLikeData = async () => {

// 退出分页判断

if (finish.value === true) {

return uni.showToast({ icon: 'none', title: '没有更多数据~' })

}

const res = await getHomeGoodsGuessLikeAPI(pageParams)

// 数组追加

guessList.value.push(...res.result.items)

// 分页条件

if (pageParams.page < res.result.pages) {

// 页码累加

pageParams.page++

} else {

finish.value = true

}

}

// 组件挂载完毕

onMounted(() => {

getHomeGoodsGuessLikeData()

})

// 暴露方法

defineExpose({

getMore: getHomeGoodsGuessLikeData,

})

</script>

<template>

<!-- 猜你喜欢 -->

<view class="caption">

<text class="text">猜你喜欢</text>

</view>

<view class="guess">

<navigator

class="guess-item"

v-for="item in guessList"

:key="item.id"

:url="`/pages/goods/goods`"

>

<image class="image" mode="aspectFill" :src="item.picture"></image>

<view class="name"> {{ item.name }} </view>

<view class="price">

<text class="small">¥</text>

<text>{{ item.price }}</text>

</view>

</navigator>

</view>

<view class="loading-text">

{{ finish ? '亲,我也是有底线的哦~' : '正在加载...' }}

</view>

</template>

效果图:看浏览器 network 面板的接口调用情况

滚动到底部,无数据

相关推荐
小胖墩有点瘦1 小时前
基于RNN+微信小程序+Flask的古诗词生成应用
微信小程序·小程序·flask
帅次3 小时前
Flutter:StatelessWidget vs StatefulWidget 深度解析
android·flutter·ios·小程序·swift·webview·android-studio
帅次6 小时前
Flutter Widget 体系结构解析
android·flutter·ios·小程序·xcode·web app·dalvik
郑州拽牛科技14 小时前
开发社交陪玩app小程序
大数据·微信小程序·小程序·系统架构·开源软件
源码姑娘18 小时前
基于Nodejs的火车订票小程序(源码+部署教程)
小程序·毕业设计
HerayChen19 小时前
uniapp vue3 微信小程序 uni.chooseLocation使用
微信小程序·小程序·uni-app·微信小程序地理位置
然后就去远行吧20 小时前
小程序 wxml 语法 —— 36 wxml 语法 - setData() 修改数据
小程序
说私域21 小时前
基于定制开发开源AI智能名片S2B2C商城小程序的零售运营策略研究
人工智能·小程序·开源·零售
低代码布道师1 天前
加油站小程序实战教程08动态获取城市和站点信息
低代码·小程序
—Qeyser1 天前
用Deepseek写一个五子棋微信小程序
微信小程序·小程序