Browse Source

猜你喜欢滑动分页

template
liruiqiang 2 months ago
parent
commit
5b6ed0b2c1
  1. 36
      src/components/XtxGuess.vue
  2. 13
      src/pages/index/index.vue
  3. 3
      src/types/component.d.ts
  4. 8
      src/types/global.d.ts

36
src/components/XtxGuess.vue

@ -18,26 +18,58 @@
</view> </view>
</navigator> </navigator>
</view> </view>
<view class="loading-text"> 正在加载... </view>
<view class="loading-text">
{{ finish ? '没有更多数据~' : '正在加载...' }}
</view>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import type { GuessItem } from '@/types/home' import type { GuessItem } from '@/types/home'
import { getHomeGoodsGuessLikeAPI } from '@/services/home' import { getHomeGoodsGuessLikeAPI } from '@/services/home'
import { onMounted, ref } from 'vue' import { onMounted, ref } from 'vue'
import type { PageParams } from '@/types/global'
//
const pageParams: Required<PageParams> = {
page: 1,
pageSize: 10,
}
// //
const guessList = ref<GuessItem[]>([]) const guessList = ref<GuessItem[]>([])
//
const finish = ref(false)
// //
const getHomeGoodsGuessLikeData = async () => { const getHomeGoodsGuessLikeData = async () => {
// 退
if (finish.value === true) {
return uni.showToast({ icon: 'none', title: '没有更多数据~' })
}
const res = await getHomeGoodsGuessLikeAPI() const res = await getHomeGoodsGuessLikeAPI()
guessList.value = res.result.items
// guessList.value = res.result.items
//
guessList.value.push(...res.result.items)
//
if (pageParams.page < res.result.pages) {
//
pageParams.page++
} else {
finish.value = true
}
} }
// //
onMounted(() => { onMounted(() => {
getHomeGoodsGuessLikeData() getHomeGoodsGuessLikeData()
}) })
//
defineExpose({
getMore: getHomeGoodsGuessLikeData,
})
</script> </script>
<style lang="scss"> <style lang="scss">

13
src/pages/index/index.vue

@ -2,7 +2,7 @@
<view class="viewport"> <view class="viewport">
<!-- 自定义导航栏 --> <!-- 自定义导航栏 -->
<CustomNavbar /> <CustomNavbar />
<scroll-view class="scroll-view" scroll-y>
<scroll-view @scrolltolower="onScrolltolower" class="scroll-view" scroll-y>
<PageSkeleton v-if="isLoading" /> <PageSkeleton v-if="isLoading" />
<!-- 自定义轮播图 --> <!-- 自定义轮播图 -->
<XtxSwiper :list="bannerList" /> <XtxSwiper :list="bannerList" />
@ -11,7 +11,7 @@
<!-- 热门推荐 --> <!-- 热门推荐 -->
<HotPanel :list="hotList" /> <HotPanel :list="hotList" />
<!-- 猜你喜欢 --> <!-- 猜你喜欢 -->
<XtxGuess />
<XtxGuess ref="guessRef" />
</scroll-view> </scroll-view>
</view> </view>
</template> </template>
@ -24,6 +24,7 @@ import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app' import { onLoad } from '@dcloudio/uni-app'
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home' import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home' import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import type { XtxGuessInstance } from '@/types/component'
// //
const bannerList = ref<BannerItem[]>([]) const bannerList = ref<BannerItem[]>([])
@ -49,6 +50,14 @@ const getHomeHotData = async () => {
// //
const isLoading = ref(false) const isLoading = ref(false)
//
const guessRef = ref<XtxGuessInstance>()
//
const onScrolltolower = () => {
guessRef.value?.getMore()
}
// //
onLoad(async () => { onLoad(async () => {
isLoading.value = true isLoading.value = true

3
src/types/component.d.ts

@ -3,6 +3,7 @@
* *
* declare module 'vue' * declare module 'vue'
*/ */
import type { type } from 'os'
import 'vue' import 'vue'
declare module 'vue' { declare module 'vue' {
export interface GlobalComponents { export interface GlobalComponents {
@ -10,3 +11,5 @@ declare module 'vue' {
XtxGuess: typeof XtxGuess XtxGuess: typeof XtxGuess
} }
} }
export type XtxGuessInstance = InstanceType<typeof XtxGuess>

8
src/types/global.d.ts

@ -11,3 +11,11 @@ export type PageResult<T> = {
/** 每页条数 */ /** 每页条数 */
pageSize: number pageSize: number
} }
/** 通用分页参数类型 */
export type PageParams = {
/** 页码:默认值为 1 */
page?: number
/** 页大小:默认值为 10 */
pageSize?: number
}
Loading…
Cancel
Save