1、引入组件定义参数
import { PullToRefresh, List, Image, InfiniteScroll, Empty } from 'antd-mobile';
interface States {
list: any
hasMore: boolean
params: GoodsListDto
}
this.state = {
list: [],
hasMore: false,
params: {
pageSize: 10,
pageNum: 1
}
}
2、请求列表接口
componentDidMount() {
this.queryList(this.state.params)
}
queryList(params: GoodsListDto): Promise<ResultPageResultTestGoodsVo> {
this.setState({ params });
return new Promise((resolve, reject) => {
api.mockGoodsList(params)
.then((res: ResultPageResultTestGoodsVo) => {
if (res && res?.data?.content) {
this.setState({
list: res.data.content,
hasMore: res.data.content.length > 0
})
}
this.setState({ hasMore: false })
resolve(res)
})
.catch((res: ResultPageResultTestGoodsVo) => {
this.setState({ hasMore: false })
reject(res)
})
})
}
3、加载更多函数/loadMore
loadMore
需要返回 promise
hasmore
结束 loadmore
函数
async loadMore() {
const res = await this.queryList({
...this.state.params,
pageNum: this.state.params.pageNum + 1
})
if (res && res.data?.content) {
this.setState({ hasMore: res.data.content.length >= 10 })
}
}
4、render(){} 渲染
<PullToRefresh
onRefresh={async () => {
this.queryList({ ...params, pageNum: 1, })
}}
>
<List style={{ '--font-size': '14px' }}>
{
list.map((item: TestGoodsVo) => (
<List.Item
style={{ border: 0 }}
key={item.id}
arrow={false}
onClick={() => { this.handleDetail(item) }}
prefix={
<Image
src={item.goodsPic || ''}
style={{ borderRadius: 5 }}
fit='cover'
width={120}
height={120}
/>
}
description={
<div>
<p className="des mt5">{item.goodsDesc}</p>
<p className="amount">¥{item.goodsAmt}</p>
<p className="tip">{item.goodsInfo}</p>
</div>
}
>
{item.goodsDesc}
</List.Item>
))
}
</List>
<InfiniteScroll loadMore={this.loadMore.bind(this)} hasMore={hasMore} threshold={100} />
</PullToRefresh>
5、视图
