uni-app实现上拉加载

 

参考文档代码:

 1 <template>
 2   <view>
 3     <!-- 省略其他内容 -->
 4     <view v-for="item in dataList" :key="item.id">{{ item.title }}</view>
 5   </view>
 6 </template>
 7 
 8 <script>
 9   export default {
10     data () {
11       return {
12         dataList: [], // 存储数据列表
13         pageNum: 1, // 当前页数
14       }
15     },
16     onReachBottom () {
17       this.requestNextPageData()
18     },
19     methods: {
20       async requestNextPageData () {
21         const res = await uni.request({
22           url: 'your-api-url',
23           data: {
24             pageNum: this.pageNum,
25           },
26         })
27         if (res.statusCode === 200) {
28           const nextDataList = res.data // 假设接口返回数据格式为 { data: [...] }
29           if (nextDataList.length > 0) {
30             this.dataList = this.dataList.concat(nextDataList)
31             this.pageNum++
32           } else {
33             uni.showToast({
34               title: '没有更多了',
35               icon: 'none',
36             })
37           }
38         } else {
39           uni.showToast({
40             title: '请求失败,请重试',
41             icon: 'none',
42           })
43         }
44       },
45     },
46   }
47 </script>

 

 

 

自己的代码:

//上拉加载
        onReachBottom() {
            if (!this.pullUpOn) {
                return;
            }

            //this.loadPosts(false);
        },
// 点击栏目切换当前页时改变样式
            swichNav(e) {
                let cur = e.currentTarget.dataset.current;

                if (this.currentTab == cur) {
                    return false;
                }

                this.currentTab = cur;
                // this.posts = [];
                this.loadPosts(true);
            },
//根据id获取某个分类下文章
            loadPosts(refresh) {
                this.loadding = true;
                Rest.get(Api.JIANGQIE_POSTS_CATEGORY+'&catid='+this.tabbar[this.currentTab]['id']+'&pagesize=50&more=1', {
                    //'offset': refresh ? 0 : this.posts.length,
                    //'sort': this.sorts[this.currentTab]
                }).then(res => {                    
                    this.loaded = true;
                    this.loadding = false;
                    this.posts = (refresh ? res.data : this.posts.concat(res.data));
                    //this.pullUpOn = (res.data.length >= Constants.JQ_PER_PAGE_COUNT);
                    //this.pageNum++;
                    console.log('栏目id',this.tabbar[this.currentTab]['id']);
                    console.log('栏目id文章',res.data);
                });
            }
<!-- 顶部栏目 -->
        <scroll-view scroll-x scroll-with-animation class="tab-view" :scroll-left="0">
            <view v-for="(item, index) in tabbar" :key="index" class="tab-bar-item"
                :class="(currentTab==index ? 'active' : '')" :data-current="index"
                @tap.stop="swichNav">
                <text class="tab-bar-title">{{item.name}}</text>
            </view>
        </scroll-view>

 

posted @ 2024-05-13 17:40  青茶360  阅读(11)  评论(0编辑  收藏  举报