uniapp的下拉刷新和上拉加载

小鱼儿 2023-05-22 08:43 157阅读 0赞

首先来说uniapp本身有对应的上拉加载和下拉刷新对应的生命周期

现在对自己一直写着的上拉加载和下拉刷新做个总结,优化

  1. <template>
  2. <view class="appointment" v-if="yuershiSubscribeList.length > 0">
  3. <view class="list" v-for="(item,index) in yuershiSubscribeList" :key="item.id">
  4. <view class="detail">
  5. <text class="name">{ { item.name}}</text>
  6. <text class="type">类型 : <text class="color">{ { item.mom_name}}</text></text>
  7. <text class="time">预约时间 : { { parseInt(item.time) | c_time('yyyy-MM-dd hh:mm:ss')}}</text>
  8. <text class="distance">地址 : { { item.address}} </text>
  9. </view>
  10. <image class="head" :src="item.image" mode=""></image>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import teacherModelClass from '@/common/model/teacher.js';
  16. const teacherModel = new teacherModelClass();
  17. export default {
  18. data() {
  19. return {
  20. loadingText: '加载中...',
  21. loadingType: 0,
  22. contentText: {
  23. contentdown:'上拉显示更多',
  24. contentrefresh: '正在加载...',
  25. contentnomore: '没有更多数据了'
  26. },
  27. page:1,
  28. row: 10,
  29. yuershiSubscribeList:[],
  30. }
  31. },
  32. onReachBottom(){
  33. var _this = this
  34. this.getmoreinfo();
  35. },
  36. onPullDownRefresh() {
  37. this.page = 1;
  38. this.loadingType = 0;
  39. this.get_data();
  40. uni.stopPullDownRefresh();
  41. },
  42. onLoad() {
  43. this.get_data();
  44. },
  45. methods: {
  46. get_data(){
  47. let _this = this
  48. teacherModel.yuershiSubscribe(_this.page,_this.row).then(res => {
  49. console.log(res);
  50. if(res.code === 2){ return false}
  51. _this.yuershiSubscribeList = res;
  52. console.log(JSON.stringify(this.yuershiSubscribeList))
  53. });
  54. },
  55. // 加载更多
  56. getmoreinfo() {
  57. let _this = this;
  58. if (_this.loadingType !== 0) { //loadingType!=0;直接返回
  59. return false;
  60. }
  61. _this.loadingType = 1;
  62. _this.page ++;
  63. var params={
  64. page:_this.page,
  65. row: _this.row
  66. };
  67. uni.showNavigationBarLoading();//显示加载动画
  68. _this.post('v1/Yuershi/my_yuershi_subscribe',params,(data)=>{
  69. console.log(data);
  70. uni.hideNavigationBarLoading();//关闭加载动画
  71. if(data.code == 2){
  72. uni.showToast({
  73. title:'没有更多啦...',
  74. icon:'none',
  75. duration:2000
  76. })
  77. return false;
  78. }else{
  79. if (data.data == null) { //没有数据
  80. _this.loadingType = 2;
  81. uni.hideNavigationBarLoading();//关闭加载动画
  82. return;
  83. }
  84. var new_info = data.data;
  85. _this.yuershiSubscribeList = _this.yuershiSubscribeList.concat(new_info);//将数据拼接在一起
  86. _this.loadingType = 0;//将loadingType归0重置
  87. uni.hideNavigationBarLoading();//关闭加载动画
  88. }
  89. });//更多会员商品
  90. },
  91. }
  92. }
  93. </script>

下面是效果下拉刷新效果
在这里插入图片描述
下面是上拉加载效果:
在这里插入图片描述
这是防止重复刷新或者加载的代码(上锁)

  1. if (_this.loadingType !== 0) { //loadingType!=0;直接返回
  2. return false;
  3. }
  4. _this.loadingType = 1;

这是定义相关加载文字的data

  1. data() {
  2. return {
  3. loadingText: '加载中...', // 加载效果动画
  4. loadingType: 0, // 上锁
  5. contentText: {
  6. contentdown:'上拉显示更多',
  7. contentrefresh: '正在加载...',
  8. contentnomore: '没有更多数据了'
  9. },
  10. page:1,
  11. row: 10,
  12. yuershiSubscribeList:[],
  13. }
  14. },

这是对应的下拉刷新和上拉加载的生命周期(钩子),在这里调用刷新加载事件

  1. onReachBottom(){
  2. // 这是上拉加载对应的生命周期 调用 getmoreinfo()方法,该方法是对上拉加载的一些优化
  3. var _this = this
  4. this.getmoreinfo();
  5. },
  6. onPullDownRefresh() {
  7. // 这是上拉刷新的生命周期(钩子)
  8. this.page = 1;
  9. this.loadingType = 0;
  10. this.get_data();
  11. uni.stopPullDownRefresh(); // 这是停止上拉刷新的事件
  12. },

发表评论

表情:
评论列表 (有 0 条评论,157人围观)

还没有评论,来说两句吧...

相关阅读