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


使用示例
<template>
<scroll-view
scroll-y
refresher-enabled
@refresherrefresh="onRefresh"
@refresherrestore="onRestore"
@refresherabort="onAbort"
>
<!-- 滾動內(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() {
// 模擬異步請求數(shù)據(jù)
setTimeout(() => {
// 這里可以編寫刷新數(shù)據(jù)的邏輯,比如重新請求接口獲取最新數(shù)據(jù)
console.log('局部下拉刷新完成');
// 停止下拉刷新動畫
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(() => {
// 停止下拉刷新動畫
uni.stopPullDownRefresh();
}).catch((error) => {
console.error('刷新數(shù)據(jù)出錯:', error);
uni.stopPullDownRefresh();
});
}
};
</script>子組件定義刷新方法
<template>
<!-- 子組件內(nèi)容 -->
</template>
<script>
export default {
methods: {
async refreshData() {
console.log('子組件開始刷新數(shù)據(jù)');
// 這里編寫刷新數(shù)據(jù)的邏輯,比如重新請求接口獲取最新數(shù)據(jù)
try {
// 調(diào)用獲取消息的方法
await this.getData();
console.log('子組件數(shù)據(jù)刷新完成');
} catch (error) {
console.error('子組件刷新數(shù)據(jù)出錯:', error);
throw error;
}
},
// 其他方法...
}
};
</script>到此這篇關(guān)于Uniapp實現(xiàn)下拉刷新的三種方式的文章就介紹到這了,更多相關(guān)Uniapp下拉刷新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript實現(xiàn)日期格式轉(zhuǎn)換
這篇文章主要介紹了javascript實現(xiàn)日期格式轉(zhuǎn)換,非常的簡單實用,項目中經(jīng)??梢杂玫剑@里推薦給大家2014-12-12
js location.replace與location.reload的區(qū)別
js location.replace與location.reload的區(qū)別,經(jīng)常能用的到,需要的朋友可以可以下。2010-09-09
javascript中$(function() {});寫與不寫有哪些區(qū)別
javascript中$(function() {....}) 是jQuery中的經(jīng)典用法,等同于 $(document).ready(function() {....}) javascript中$(function() {});寫與不寫有哪些區(qū)別,需要的朋友可以參考下2015-08-08
javascript算法題 求任意一個1-9位不重復(fù)的N位數(shù)在該組合中的大小排列序號
從1--9中選取N個數(shù)字,組成不重復(fù)的N位數(shù),從小到大進行編號,當輸入其中任何一個數(shù)M時,能找出該數(shù)字對應(yīng)的編號2012-07-07
微信小程序?qū)崙?zhàn)之上拉(分頁加載)效果(2)
這篇文章主要介紹了微信小程序?qū)崙?zhàn)之上拉加載、分頁加載效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
JS實現(xiàn)基于Sketch.js模擬成群游動的蝌蚪運動動畫效果【附demo源碼下載】
這篇文章主要介紹了JS實現(xiàn)基于Sketch.js模擬成群游動的蝌蚪運動動畫效果,涉及Sketch.js插件的使用及HTML5元素的應(yīng)用技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-08-08

