uniapp下拉刷新和上拉加载的实现

本文详细介绍了在小程序中实现下拉刷新和上拉加载的步骤。首先在pages.json文件的globalStyle中开启下拉刷新功能,然后在goods.vue页面中定义onPullDownRefresh函数进行刷新逻辑,并在onReachBottom中处理上拉加载更多数据的逻辑。通过这种方式,实现了商品列表的动态更新和无限滚动加载。
摘要由CSDN通过智能技术生成

下拉刷新的实现步骤(代码解释详细,通俗易懂)

1.pages.json文件中,globalStyle里面开启下拉刷新,属性为true

{

    "pages": [

        {

        "path": "pages/index/index"

        },

        {

            "path": "pages/search/search"

        },

        {

            "path": "pages/cart/cart"

        },

        {

            "path": "pages/member/member"

        }

        ,{

            "path" : "pages/goods/goods",

            "style" :                                                                                    

            {

                "navigationBarTitleText": ""

                // "enablePullDownRefresh": true

            }

            

        }

    ],

    "globalStyle": {

        "navigationBarTextStyle": "white",

        "navigationBarTitleText": "多抓鱼",

        "navigationBarBackgroundColor": "#003153",

        "enablePullDownRefresh": true,

        "backgroundColor": "#F8F8F8",

        "app-plus": {

            "background": "#efeff4"

        }

    },

    "tabBar": {

        //列表的顺序决定图标的顺序

        "list": [

            {

                "pagePath":"pages/index/index",

                "text":"首页",

                "iconPath":"static/icon/home.png",

                "selectedIconPath":"static/icon/home-active.png"

            },

            {

                "pagePath":"pages/search/search",

                "text":"搜索",

                "iconPath":"static/icon/news.png",

                "selectedIconPath":"static/icon/news-active.png"

            },

            {

                "pagePath":"pages/cart/cart",

                "text":"购物车",

                "iconPath":"static/icon/cart.png",

                "selectedIconPath":"static/icon/cart-active.png"

            },

            {

                "pagePath":"pages/member/member",

                "text":"会员",

                "iconPath":"static/icon/member.png",

                "selectedIconPath":"static/icon/member-active.png"

            }

        ]


    }


}

2.onPullDownRefresh是页面周期函数,下拉操作会触发该函数

goods.vue中

methods: {

            async getGoodsInfo(callback){

                const res = await this.$myRequest({

                    url: "/goods/search?pageIndex="+this.pageIndex,

                    method: 'get',

                    

                })

                //对象扩展运算符,将两个数组合并成一个

                this.goods = [...this.goods,...res.data.message.goods];

                this.total = res.data.message.total;

                this.pagenum = res.data.message.pagenum;

                console.log("total",this.goods.length)

                //传入回调函数就执行

                callback && callback()

            }

        },

onPullDownRefresh() {

                    console.log('下拉刷新了')

                    this.pageindex = 1

                    this.goods = []

                    this.flag = false

                    setTimeout(()=>{

                      this.getGoodsInfo(()=>{

                            uni.stopPullDownRefresh()

                        })    

                    },1000)

        }

上拉加载

页面周期函数,页面拉到底自动触发。

methods: {

            async getGoodsInfo(callback){

                const res = await this.$myRequest({

                    url: "/goods/search?pageIndex="+this.pageIndex,

                    method: 'get',

                    

                })

                this.goods = [...this.goods,...res.data.message.goods];

                this.total = res.data.message.total;

                this.pagenum = res.data.message.pagenum;

                console.log("total",this.goods.length)

                callback && callback()

            }

        },

onReachBottom() {

            if(this.goods.lenth < 20*this.pageIndex){

                return this.flag = true;    

            }

            this.pageIndex++;

            this.getGoodsInfo();

        },

uniapp中,可以使用`uni-scroll-view`组件实现加载下拉刷新。 1. 首先,在页面中引入`uni-scroll-view`组件。 ```html <template> <uni-scroll-view ref="scroll-view" class="scroll-view" :scroll-with-animation="true" @scrolltolower="loadMore" @scrolltoupper="refresh"> <!--内容区--> </uni-scroll-view> </template> ``` 2. 在`script`中定义`loadMore`和`refresh`方法。 ```javascript <script> export default { methods: { // 加载更多 loadMore() { // TODO: 加载更多的逻辑 }, // 下拉刷新 refresh() { // TODO: 下拉刷新的逻辑 } } } </script> ``` 3. 在`loadMore`方法中,可以通过调用`uni.request`方法向后端请求更多数据,并将新数据追加到原有数据的末尾。同时,可以设置一个`loading`变量,在数据请求完成之前显示一个加载提示。 ```javascript // 加载更多 loadMore() { // 如果正在加载数据,则返回 if (this.loading) return // 显示加载提示 this.loading = true uni.request({ url: 'https://xxx.com/api/list', data: { page: this.page + 1 }, success: res => { // 将新数据追加到原有数据的末尾 this.list = this.list.concat(res.data.list) // 更新页码 this.page++ // 隐藏加载提示 this.loading = false }, fail: res => { // 隐藏加载提示 this.loading = false } }) } ``` 4. 在`refresh`方法中,可以通过调用`uni.request`方法重新请求数据,并将新数据覆盖原有数据。同时,可以设置一个`refreshing`变量,在数据请求完成之前显示一个刷新提示。 ```javascript // 下拉刷新 refresh() { // 如果正在刷新,则返回 if (this.refreshing) return // 显示刷新提示 this.refreshing = true uni.request({ url: 'https://xxx.com/api/list', data: { page: 1 }, success: res => { // 将新数据覆盖原有数据 this.list = res.data.list // 更新页码 this.page = 1 // 隐藏刷新提示 this.refreshing = false }, fail: res => { // 隐藏刷新提示 this.refreshing = false } }) } ``` 5. 最后,可以通过`uni-loading`组件和`uni-load-more`组件分别实现加载提示和加载更多提示。 ```html <template> <uni-scroll-view ref="scroll-view" class="scroll-view" :scroll-with-animation="true" @scrolltolower="loadMore" @scrolltoupper="refresh"> <!--内容区--> <uni-load-more :show="loading"> 加载中... </uni-load-more> </uni-scroll-view> <uni-loading :show="refreshing" mode="pulling"> 正在刷新... </uni-loading> </template> ``` 这样,就可以在uniapp实现加载下拉刷新了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值