3

The following introduces a Vue mobile terminal pull-down refresh and pull-up loading component, which is small in size and runs fast, and is very simple to use.

A pull-to-refresh and pull-up load component for Vue.js.

Easy to use by providing simple api. Unlike other component libraries, it uses the browser itself instead of js for the scrolling container, so its code size is smaller without losing user experience.

Chinese | English

preview

demo

The demo can also be accessed by scanning the QR code below:

<img src="https://user-images.githubusercontent.com/20060839/145163261-02025f86-ac87-4016-859f-15677a6d3cf7.png" width="220" height="220" >

Install & use

Install npm package

# npm
npm install vuejs-loadmore --save

global import

import Vue from 'vue';
import VueLoadmore from 'vuejs-loadmore';

Vue.use(VueLoadmore);

International support

Support Chinese zh-CN and English en-US, the default is zh-CN.

import VueLoadmore from 'vuejs-loadmore';

Vue.use(VueLoadmore, {
  lang: 'en-US'
})

You can also specify the language locale.use()

import VueLoadmore, { locale } from 'vuejs-loadmore';

Vue.use(VueLoadmore);
locale.use('en-US');

usage

Basic usage

<vue-loadmore 
  :on-refresh="onRefresh" 
  :on-loadmore="onLoad"
  :finished="finished">
  <div v-for="(item, i) of list" :key="i"></div>
</vue-loadmore>

on-refresh and on-loadmore will be triggered when pull down to refresh or scroll to the bottom, and the callback function done() needs to be executed after processing the data request.

If you don't need to refresh, just unbind on-refresh .

When the data request is completed, you can change finished to true, which will show there is no more for 161ee24507fa4f.

export default {
  data() {
    return {
      list: [],
      page: 1,
      pageSize: 10,
      finished: false
    };
  },
  mounted() {
    this.fetch();
  },
  methods: {
    onRefresh(done) {
      this.list = [];
      this.page = 1;
      this.finished = false;
      this.fetch();

      done();
    },

    onLoad(done) {
      if (this.page <= 10) {
        this.fetch();
      } else {
        // all data loaded
        this.finished = true;
      }
      done();
    },

    fetch() {
      for (let i = 0; i < this.pageSize; i++) {
        this.list.push(this.list.length + 1);
      }
      this.page++;
    }
  },
}

Error message

<vue-loadmore 
  :on-loadmore="onLoad"
  :finished="finished"
  :error.sync="error">
  <div v-for="(item, i) of list" :key="i"></div>
</vue-loadmore>
export default {
  data() {
    return {
      list: [],
      finished: false,
      error: false,
    };
  },
  methods: {
    onLoad() {
      fetchSomeThing().catch(() => {
        this.error = true;
      });
    },
  },
};

API

Props

AttributeDescriptionTypeDefault
on-refreshtop pull down triggerfunction-
pulling-textdrop down display textstring pull down to refresh
loosing-textrelease display textstring release refresh
loading-textRefreshing display textstring is refreshing
success-textRefresh complete display textstring refresh completed
show-success-textWhether to display success-textbooleantrue
pull-distanceThe distance to trigger the Refreshing state_number \string_50
head-heightRefreshing the height of the display area_number \string_50
animation-durationPull down to refresh animation duration_number \string_200
on-loadmoreScroll to bottom triggerfunction-
immediate-checkWhether to check immediately after mountedbooleantrue
load-offsetWhen the distance from the scroll bar to the bottom is less than load-offset on-loadmore will be issued_number \string_50
finishedWhether the data is loaded, if changed to true, it will display finished-textbooleanfalse
errorWhether the data is loaded incorrectly, on-loadmore is only triggered when the error text is clicked, the sync modifier is requiredbooleanfalse
loading-textscroll to bottom loading display textstring is loading
finished-textScroll to bottom loaded display textstring no more
error-textLoading error display textstring request failed, click reload

method

Use ref to get the List instance and call the instance method.

NameDescription
checkScrollCheck the current scroll position, if it has scrolled to the bottom, it will trigger on-loadmore

example

Check out the demo for a quick overview of how to use this package.

git clone git@github.com:staticdeng/vuejs-loadmore.git
yarn install
yarn example:dev

听过千百首歌
461 声望11 粉丝