二次封装mint-ui的loadMore组件

648 阅读1分钟

二次封装mint-ui的loadMore组件

组件loadMore.vue
<template>
  <div class="moreListDiv" ref="wrapper" :style="{ height: (wrapperHeight) + 'px' }">
    <mt-loadmore :top-method="loadTop" :bottom-method="loadBottom" :bottom-all-loaded="allLoaded" ref="loadmore" :autoFill="isAutoFill" :bottomDistance="30">
      <slot name="content"></slot>
    </mt-loadmore>
  </div>
</template>

<script>
export default {
  props: ["allLoaded"],

  data() {
    return {
      // allLoaded: false, // 可以进行上拉
      isAutoFill: false, // 是否自动触发上拉函数
      wrapperHeight: 0
    };
  },

  mounted() {
    // 父控件要加上高度,否则会出现上拉不动的情况
    this.wrapperHeight =
      document.documentElement.clientHeight -
      this.$refs.wrapper.getBoundingClientRect().top;
  },

  methods: {
    // 下拉刷新
    loadTop() {
      // this.loadFrist();
      this.$parent.loadFrist();
      console.log("子=>父");
    },

    // 上拉加载
    loadBottom() {
      setTimeout(() => {
        // this.loadMoreData();
        this.$parent.loadMoreData();
      }, 1000);
    }
  }
};
</script>

<style>
.moreListDiv {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
</style>


global.js
// 下拉加载完的处理方法
initTopLoad() {
  setTimeout(() => {
    let child = this.$children;
    let child2 = child[0].$children;
    child2[0].onTopLoaded();
  }, 1000);
},

// 列表加载处理分页方法
initBottomLoad() {
  let child = this.$children;
  let child2 = child[0].$children;
  child2[0].onBottomLoaded();
  if (this.params.pageNum == 1) {
    $(".moreListDiv").animate({ scrollTop: 0 }, 20);
  }
  this.loading = false;
}
example(举个例子)
html
    <!-- 列表 -->
    <loadMore :allLoaded="allLoaded">
      <div slot="content">
        <ul class="notificationList">
          <li v-for="(item,i) in infoList" :key="i" @click="goDetail(item.informationId)">
            <span>{{item.title}}</span>
            <p class="updatedDate">更新时间:{{item.releaseDate}}</p>
          </li>
        </ul>

        <!-- 列表数据过少,影响下拉刷新(临时解决方案) -->
        <div class="zwLoadmore" v-show="infoList.length<4&&infoList.length>0"></div>

        <!-- 无数据时的显示 -->
        <div class="noDataDiv" v-if="infoList.length===0&&!loading">
          <div>
            <div class="noData">
              <img class="img" src="http://hnnmy.oss-cn-shanghai.aliyuncs.com/file/demand/static/2019-06-13/484fc1c3-82e6-4d86-8fcd-26cf8b941ffe.png" alt="">
            </div>
            <p class="txt">查询无数据</p>
          </div>
        </div>

        <div class="loadMoreGif" v-if="params.pageNum>1&&allLoaded">
          <p>没有更多了</p>
        </div>
      </div>
    </loadMore>
js
// 下拉刷新加载
loadFrist() {
  this.params.pageNum = 1;
  this.$store.dispatch("noticeList/loadFristDeep", this.params).then(() => {
    this.initTopLoad();
  });
},

// 加载更多
loadMoreData() {
  this.params.pageNum = this.params.pageNum + 1;
  this.$store
    .dispatch("noticeList/loadMoreDataDeep", this.params)
    .then(() => {
      this.initBottomLoad();
    });
}

原创@昭阳。