Uniapp實(shí)現(xiàn)下拉刷新的三種方式
在小程序、h5等地方中,常常會(huì)用到下拉刷新這個(gè)功能,今天來講解實(shí)現(xiàn)這個(gè)功能的三種方式:全局下拉刷新,組件局部下拉刷新,嵌套組件下拉刷新。
全局下拉刷新
這個(gè)方式簡單,性能佳,最推薦,以下為步驟:
配置pages.json(在需要該功能的頁面設(shè)置對(duì)應(yīng)屬性)
{
"pages": [
{
"path": "pages/index/index",
"style": {
"enablePullDownRefresh": true,
// 下拉 loading 的樣式,可選值為 'dark' 或 'light'
"backgroundTextStyle": "dark"
}
}
]
}在頁面中監(jiān)聽下拉刷新時(shí)間(使用onPullDownRefresh生命周期函數(shù))
<template>
<view>
<!-- 頁面內(nèi)容 -->
</view>
</template>
<script>
export default {
onPullDownRefresh() {
// 模擬異步請(qǐng)求數(shù)據(jù)
setTimeout(() => {
// 這里可以編寫刷新數(shù)據(jù)的邏輯,比如重新請(qǐng)求接口獲取最新數(shù)據(jù)
console.log('下拉刷新完成');
// 停止下拉刷新動(dòng)畫
uni.stopPullDownRefresh();
}, 2000);
}
};
</script>scroll-view 組件局部下拉刷新
- 在
scroll-view組件中有自定義下拉刷新的屬性以及相關(guān)方法可以直接使用,但是性能不如全局下拉刷新,且scroll-view組件停止下拉刷新的方法可能有兼容問題,會(huì)使用不了,此時(shí)可以用refressher-triggered屬性控制下拉刷新的狀態(tài)。 - 注意:scroll-view是區(qū)域滾動(dòng),不會(huì)觸發(fā)頁面滾動(dòng),無法觸發(fā)pages.json配置的下拉刷新、頁面觸底o(hù)nReachBottomDistance、titleNView的transparent透明漸變。


使用示例
<template>
<scroll-view
scroll-y
refresher-enabled
@refresherrefresh="onRefresh"
@refresherrestore="onRestore"
@refresherabort="onAbort"
>
<!-- 滾動(dòng)內(nèi)容 -->
<view v-for="item in list" :key="item.id">{{ item.name }}</view>
</scroll-view>
</template>
<script>
export default {
data() {
return {
list: [
{ id: 1, name: 'Item 1' },
{ id: 2, name: 'Item 2' },
{ id: 3, name: 'Item 3' }
]
};
},
methods: {
onRefresh() {
// 模擬異步請(qǐng)求數(shù)據(jù)
setTimeout(() => {
// 這里可以編寫刷新數(shù)據(jù)的邏輯,比如重新請(qǐng)求接口獲取最新數(shù)據(jù)
console.log('局部下拉刷新完成');
// 停止下拉刷新動(dòng)畫
this.$refs.scrollViewRef.finishPullToRefresh();
}, 2000);
},
onRestore() {
console.log('下拉刷新被復(fù)位');
},
onAbort() {
console.log('下拉刷新被中止');
}
}
};
</script>嵌套組件中的下拉刷新
場景:需要在子組件觸發(fā)下拉刷新功能,但是在pages.json中只能配置父頁面的下拉刷新屬性
父組件配置全局下拉刷新
在page.json中為父頁面配置enablePullDownRefresh為true,并在父組件的onPullDownRefresh生命周期函數(shù)中調(diào)用子組件的刷新方法。
<template>
<view>
<!-- 其他內(nèi)容 -->
<child-component ref="childRef"></child-component>
</view>
</template>
<script>
import ChildComponent from './ChildComponent.vue';
export default {
components: {
ChildComponent
},
onPullDownRefresh() {
console.log('父頁面觸發(fā)下拉刷新');
// 調(diào)用子組件的刷新方法
this.$refs.childRef.refreshData().then(() => {
// 停止下拉刷新動(dòng)畫
uni.stopPullDownRefresh();
}).catch((error) => {
console.error('刷新數(shù)據(jù)出錯(cuò):', error);
uni.stopPullDownRefresh();
});
}
};
</script>子組件定義刷新方法
<template>
<!-- 子組件內(nèi)容 -->
</template>
<script>
export default {
methods: {
async refreshData() {
console.log('子組件開始刷新數(shù)據(jù)');
// 這里編寫刷新數(shù)據(jù)的邏輯,比如重新請(qǐng)求接口獲取最新數(shù)據(jù)
try {
// 調(diào)用獲取消息的方法
await this.getData();
console.log('子組件數(shù)據(jù)刷新完成');
} catch (error) {
console.error('子組件刷新數(shù)據(jù)出錯(cuò):', error);
throw error;
}
},
// 其他方法...
}
};
</script>到此這篇關(guān)于Uniapp實(shí)現(xiàn)下拉刷新的三種方式的文章就介紹到這了,更多相關(guān)Uniapp下拉刷新內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript實(shí)現(xiàn)手勢識(shí)別的示例詳解
這篇文章主要為大家詳細(xì)介紹了JavaScrip如何利用?TensorFlow.js?中的?HandPose?模型,實(shí)現(xiàn)一個(gè)基于視頻流的手勢識(shí)別系統(tǒng),感興趣的可以了解下2024-12-12
cropper js基于vue的圖片裁剪上傳功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了cropper js基于vue的圖片裁剪上傳功能的相關(guān)資料,需要的朋友可以參考下2018-03-03
JS在IE和FireFox之間常用函數(shù)的區(qū)別小結(jié)
IE和FireFox之間常用函數(shù)的區(qū)別小結(jié),需要的朋友可以參考下。2010-03-03
微信小程序?qū)崿F(xiàn)判斷是分享到群還是個(gè)人功能示例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)判斷是分享到群還是個(gè)人功能,結(jié)合實(shí)例形式分析了微信小程序分享與判斷功能的實(shí)現(xiàn)原理、步驟及相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
微信小程序?qū)W習(xí)筆記之登錄API與獲取用戶信息操作圖文詳解
這篇文章主要介紹了微信小程序?qū)W習(xí)筆記之登錄API與獲取用戶信息操作,結(jié)合實(shí)例形式分析了微信小程序登陸請(qǐng)求及后臺(tái)交互相關(guān)操作技巧,并結(jié)合圖文形式進(jìn)行說明,需要的朋友可以參考下2019-03-03

