網站首頁 編程語言 正文
實現上拉加載更多
-
打開項目根目錄中的
pages.json
配置文件,為subPackages
分包中的商品?goods_list
頁面配置上拉觸底的距離:
"subPackages": [ { "root": "subpkg", "pages": [ { "path": "goods_detail/goods_detail",
"style": {} }, { "path": "goods_list/goods_list", "style": { "onReachBottomDistance":
150 } }, { "path": "search/search", "style": {} } ] } ]
-
在?
goods_list
?頁面中,和?methods
?節點平級,聲明?onReachBottom
?事件處理函數,用來監聽頁面的上拉觸底行為:
// 觸底的事件 onReachBottom() { // 讓頁碼值自增 +1 this.queryObj.pagenum += 1 //
重新獲取列表數據 this.getGoodsList() }
-
?改造?
methods
?中的?getGoodsList
?函數,當列表數據請求成功之后,進行新舊數據的拼接處理:
// 獲取商品列表數據的方法 async getGoodsList() { // 發起請求 const { data: res } = await
uni.$http.get('/api/public/v1/goods/search', this.queryObj) if (res.meta.status
!== 200) return uni.$showMsg() // 為數據賦值:通過展開運算符的形式,進行新舊數據的拼接 this.goodsList
= [...this.goodsList, ...res.message.goods] this.total = res.message.total
}
優化:
通過節流閥防止發起額外的請求?
-
在 data 中定義?
isloading
?節流閥如下:
data() { return { // 是否正在請求數據 isloading: false } }
-
?修改?
getGoodsList
?方法,在請求數據前后,分別打開和關閉節流閥:
// 獲取商品列表數據的方法 async getGoodsList() { // ** 打開節流閥 this.isloading = true
// 發起請求 const { data: res } = await uni.$http.get('/api/public/v1/goods/search',
this.queryObj) // ** 關閉節流閥 this.isloading = false // 省略其它代碼... }
-
在?
onReachBottom
?觸底事件處理函數中,根據節流閥的狀態,來決定是否發起請求:
// 觸底的事件 onReachBottom() { // 判斷是否正在請求其它數據,如果是,則不發起額外的請求 if (this.isloading)
return this.queryObj.pagenum += 1 this.getGoodsList() }
?判斷數據是否加載完畢
- 如果下面的公式成立,則證明沒有下一頁數據了:
當前的頁碼值 * 每頁顯示多少條數據 >= 總數條數 pagenum * pagesize >= total
-
修改?
onReachBottom
?事件處理函數如下:
// 觸底的事件 onReachBottom() { // 判斷是否還有下一頁數據 if (this.queryObj.pagenum *
this.queryObj.pagesize >= this.total) return uni.$showMsg('數據加載完畢!')
// 判斷是否正在請求其它數據,如果是,則不發起額外的請求 if (this.isloading) return this.queryObj.pagenum
+= 1 this.getGoodsList() }
原文鏈接:https://blog.csdn.net/SmartJunTao/article/details/123684356
相關推薦
- 2022-06-02 C語言結構體(struct)的詳細講解_C 語言
- 2022-11-06 Git?Commitizen提交規范化自動生成changelog文件_相關技巧
- 2022-01-21 有了 IP 地址,為什么還要用 MAC 地址?
- 2022-10-07 Android?drawFunctor?原理及應用詳情_Android
- 2022-05-23 高效的數據同步工具DataX的使用及實現示例_數據庫其它
- 2022-04-23 Tooltip 組件:根據內容是否溢出判斷是否顯示 Tooltip
- 2022-06-20 k8s在go語言中的使用及client?初始化簡介_Golang
- 2022-05-15 Docker基本概念和底層原理解析_docker
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支