// 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 面板的接口調用情況
滾動到底部,無數據