Beginner vue -- realize the function of pull-up loading pull-down refresh based on vant mobile components

catalogue

1. Pull up loading

2. Pull down refresh

3. Synchronous use of pull-up loading and pull-down refresh

4.props API

 1. Pull up loading

<van-list
  v-model="loading"
  :finished="finished"
  finished-text="No more"
  @load="onLoad"
>
  <van-cell v-for="item in list" :key="item" :title="item" />
</van-list>
export default {
  data() {
    return {
      list:[],
      loading: true,
      finished: false
    }
  },
methods: {
    onLoad() {
      // Update data asynchronously
      // An axios request function
      const {data:res} = await initList(){}

      //Store data behind array
      this.list = [...this.list,...res]

      // End of loading status
      this.loading = false

      //If the requested data is empty
      if (res.length === 0) {
        this.finished = true
      }
  },
};

Among them, loading is a Boolean value, which represents the loading state. When we start loading, its value changes from false to true. When the request ends, its value changes from true to false.

Similarly, finish is also a Boolean value, which represents whether the data has been fully loaded. If the data has been fully loaded and there is no new data to request, the value of finished is changed to true.

The value of finished text is the prompt when it is fully loaded.

Note: when the default value of loading is false, an onLoad event will be triggered automatically when the page is just loaded. When the default value is true, it will not be triggered.

2. Pull up refresh

<van-pull-refresh v-model="isLoading" @refresh="onRefresh">
    <van-cell v-for="item in list" :key="item" :title="item" />
</van-pull-refresh>
export default {
  data() {
    return {
      list:[],
      Loading: false,
      finished: false
    }
  },
  methods: {
    onRefresh() {
      // Update data asynchronously
      // An axios request function
      const {data:res} = await initList(){}

      //Store data in front of the array
      this.list = [...res,...this.list]

      // End of loading status
      this.loading = false

      //If the requested data is empty
      if (res.length === 0) {
        this.finished = true
      }
  }
};

Among them, loading is a Boolean value, which represents the refresh status. When we start the refresh, its value changes from false to true. When the refresh ends, its value changes from true to false.

Similarly, finish is also a Boolean value, which represents whether the data has been fully loaded. If the data has been fully loaded and there is no new data to request, the value of finished is changed to true.

3. Synchronous use of pull-up loading and pull-down refresh

<van-pull-refresh v-model="refreshing" @refresh="onRefresh" :disabled='finished'>
  <van-list
    v-model="loading"
    :finished="finished"
    finished-text="No more"
    @load="onLoad"
  >
    <van-cell v-for="item in list" :key="item" :title="item" />
  </van-list>
</van-pull-refresh>
export default {
  data() {
    return {
      list: [],
      loading: true,
      finished: false,
      refreshing: false,
    };
  },
  methods: {
    onLoad() {
      // Update data asynchronously
      // An axios request function
      const {data:res} = await initList(){}

      //Store data behind array
      this.list = [...this.list,...res]

      // End of loading status
      this.loading = false

      //If the requested data is empty
      if (res.length === 0) {
        this.finished = true
    },
    onRefresh() {
      // Update data asynchronously
      // An axios request function
      const {data:res} = await initList(){}

      //Store data in front of the array
      this.list = [...res,...this.list]

      // End of loading status
      this.refreshing = false

      //If the requested data is empty
      if (res.length === 0) {
        this.finished = true
    }
  }
};

Note: in the < van pull refresh > tab, we have added the disable attribute of vant official, which is used to disable the pull-down refresh function. When all our data has been requested, that is, finished === true, the pull-down refresh is not available.

4.props API

Here we will list the props API officially provided by vant

  • List (pull-up loading)
    parameterexplaintypeDefault value

    v-model

    Whether it is in the loading state. The load event is not triggered during loadingbooleanfalse
    finishedWhether the loading is completed. The load event will not be triggered after the loading is completedbooleanfalse
    errorWhether the loading fails. After the loading fails, click the error prompt to restart
    The sync modifier must be used to trigger the load event
    booleanfalse
    offsetThe load event is triggered when the distance between the scroll bar and the bottom is less than offsetnumber | string300
    loading-textPrompt copy during loadingstringLoading
    finished-textPrompt copy after loadingstring-
    error-textPrompt copy after loading failurestring-
    immediate-checkDo you want to perform a scroll position check immediately on initializationbooleantrue
    directionThe direction of scrolling trigger loading. The optional value is upstringdown
  • PullRefresh
    parameterexplaintypeDefault value
    v-modelIs it loadingboolean-
    pulling-textDrop down process prompt copystringPull down to refresh
    loosing-textRelease process prompt copystringRelease to refresh
    loading-textLoading process prompt copystringLoading
    success-textRefresh successful prompt copystring-
    success-durationDisplay duration of refresh success prompt (ms)number | string500
    animation-durationAnimation durationnumber | string300
    head-heightTop content heightnumber | string50
    pull-distance v2.12.8Distance to trigger pull-down refreshnumber | stringConsistent with {head height}
    disabledDisable pull-down refreshbooleanfalse

Keywords: Javascript Front-end Vue.js

Added by four4swords on Mon, 17 Jan 2022 16:12:38 +0200