PullRefresh 下拉刷新
自己的滚动容器通过原生刷新能力接入;父容器必须有确定高度。普通 Vue 支持页面滚动模式。
引入
uni-app 配置 easycom 后使用 hui-pull-refresh。普通 Vue 从 @nscode/handy-uni/web 具名导入 HuiPullRefresh,或安装预览插件。
示例
交互与事件结果
操作组件后,下方显示最近一次事件及完整参数。弹层选择器先修改草稿,确认后提交;取消保留原值。
vue
<script setup lang="ts">
import { ref } from 'vue'
const result = ref('')
const items = ref([1, 2, 3])
const refresh = async () => {
items.value = [Date.now(), ...items.value]
return true
}
function event0(arg0: unknown) {
result.value = 'update:loading' + ': ' + JSON.stringify([arg0])
}
function event1() {
result.value = 'refresh' + ': ' + JSON.stringify([])
}
function event2(arg0: unknown) {
result.value = 'change' + ': ' + JSON.stringify([arg0])
}
function event3() {
result.value = 'reach-bottom' + ': ' + JSON.stringify([])
}
function event4(arg0: unknown) {
result.value = 'scroll' + ': ' + JSON.stringify([arg0])
}
</script>
<template>
<view style="height: 280px">
<hui-pull-refresh
:refresh="refresh"
@update:loading="event0"
@refresh="event1"
@change="event2"
@reach-bottom="event3"
@scroll="event4"
><view v-for="item in items" :key="item" style="padding: 20px">{{
item
}}</view></hui-pull-refresh
>
<text aria-live="polite">{{ result }}</text>
</view>
</template>
行为与平台差异
uni 的 container="page" 开发环境提示并按 self 处理。页面滚动刷新应在页面开启 enablePullDownRefresh,调用 usePaging.refresh() 并结束原生刷新。disabled 仅禁手势,startRefresh() 仍可调用。
Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
loading | boolean | undefined | undefined | 受控刷新/加载状态。 |
default-loading | boolean | undefined | false | 初始刷新状态。 |
refresh | (() => unknown) | undefined | — | 刷新函数;false、抛错与拒绝均显示失败。 |
disabled | boolean | undefined | false | 禁用用户操作;优先于表单继承配置。 |
container | "self" | "page" | undefined | 'self' | self 自身滚动;page 仅普通 Vue 页面滚动。 |
head-height | number | undefined | 50 | 刷新头停留高度 px。 |
pull-distance | number | undefined | — | 松手触发阈值 px,缺省同 headHeight。 |
pulling-text | string | undefined | — | 下拉状态文字。 |
loosing-text | string | undefined | — | 释放状态文字。 |
loading-text | string | undefined | — | 刷新中文字。 |
success-text | string | undefined | — | 成功文字;显式空字符串且无插槽时直接收起。 |
failed-text | string | undefined | — | 失败文字;显式空字符串且无插槽时直接收起。 |
success-duration | number | undefined | 500 | 成功/失败文字停留 ms。 |
lower-threshold | number | undefined | 50 | 距滚动底部多少 px 发 reach-bottom。 |
custom-class | string | undefined | '' | 根节点附加类名。 |
custom-style | string | CSSProperties | undefined | — | 根节点附加样式。 |
Events
| 事件 | 参数 | 说明 |
|---|---|---|
update:loading | (loading: boolean) | 请求更新 loading;受控模式由父级回写。 |
refresh | () | 报告 refresh,完整参数见示例事件输出。 |
change | (info: { status: PullRefreshStatus; distance: number; }) | 提交后的值与上下文,发生在模型更新事件之后。 |
reach-bottom | () | 报告 reach-bottom,完整参数见示例事件输出。 |
scroll | (info: { scrollTop: number; }) | 报告 scroll,完整参数见示例事件输出。 |
Slots
| 插槽 | 说明 |
|---|---|
default | 默认内容。 |
pulling | 刷新状态头,作用域 {distance}。 |
loosing | 刷新状态头,作用域 {distance}。 |
loading | 刷新状态头,作用域 {distance}。 |
success | 刷新状态头,作用域 {distance}。 |
failed | 刷新状态头,作用域 {distance}。 |
方法
| 方法 | 签名 |
|---|---|
startRefresh | () => Promise<void> |
scrollTo | (top: number) => void |
类型
PullRefreshProps、PullRefreshEmits、PullRefreshExpose。公开类型由包入口统一导出。
平台说明
| 范围 | 行为与验证边界 |
|---|---|
| 公共实现 | uni 目标使用平台滚动容器的刷新能力;普通 Vue 通过顶部触摸手势和方向锁识别下拉。 |
| 设备待验 | 核对页面与内部容器滚动、未到顶部不触发、加载互斥与失败恢复;iOS 真机检查背景滚动。 |
对应操作见 examples/uni-demo/smoke/PullRefresh.md。单元测试不替代真机验收。