vue 使用mint-ui實現上拉加載和下拉刷新

解決了官網中下拉刷新存在的問題ios

<template>
  <div class="tmpl">
    <nav-bar title="商品列表"></nav-bar>
    <div class="main-body" ref="wrapper" :style="{ height: (wrapperHeight-50) + 'px' }">
      <mt-loadmore :top-method="loadTop" :bottom-method="loadBottom" :bottom-all-loaded="allLoaded" ref="loadmore" :autoFill="isAutoFill">
        <ul class="mui-table-view mui-grid-view">
          <li v-for="(item,index) in datas" :key="index" class="mui-table-view-cell mui-media mui-col-xs-6">
            <a>
              <img class="mui-media-object" v-lazy="item.img">
              <div class="mui-media-body" v-text="item.title"></div>
            </a>
          </li>
        </ul>
      </mt-loadmore>
    </div>
  </div>
</template>

<script>
export default {
  name: "goodslist",
  data() {
    return {
      datas: [],
      //能夠進行上拉
      allLoaded: false,
      //是否自動觸發上拉函數
      isAutoFill: false,
      wrapperHeight: 0,
      courrentPage: 0
    };
  },
  created() {
    this.loadFrist();
  },
  mounted() {
    // 父控件要加上高度,不然會出現上拉不動的狀況
    this.wrapperHeight =
      document.documentElement.clientHeight -
      this.$refs.wrapper.getBoundingClientRect().top;
  },
  methods: {
    //   下拉刷新
    loadTop() {
      this.loadFrist();
    },
    // 上拉加載
    loadBottom() {
      this.loadMore();
    },
    // 下來刷新加載
    loadFrist() {
      this.$axios
        .get("goodslist1.json")
        .then(response => {
          this.courrentPage = 0;
          this.allLoaded = false; // 能夠進行上拉
          this.datas = response.data.message;
          this.$refs.loadmore.onTopLoaded();
        })
        .catch(error => {
          console.log(error);
          alert("網絡錯誤,不能訪問");
        });
    },
    // 加載更多
    loadMore() {
      this.$axios
        .get("goodslist.json")
        .then(response => {
          // concat數組的追加
          this.datas = this.datas.concat(response.data.message);
          if (this.courrentPage > 2) {
            this.allLoaded = true; // 若數據已所有獲取完畢
          }
          this.courrentPage++;
          this.$refs.loadmore.onBottomLoaded();
        })
        .catch(error => {
          console.log(error);
          alert("網絡錯誤,不能訪問");
        });
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.main-body {
  /* 加上這個纔會有當數據充滿整個屏幕,能夠進行上拉加載更多的操做 */
  overflow: scroll;
}
</style>
相關文章
相關標籤/搜索