Web projects often use the function of pulling down and scrolling to load data. Today, I will introduce the Vue-infinite-loading plug-in and explain how to use it! Step 1: Installationnpm install vue-infinite-loading --save Step 2: Citationimport InfiniteLoading from 'vue-infinite-loading'; export default { components: { InfiniteLoading } } Step 3: Use1. Basic usage <template> <div> <p v-for="item in list"> <span v-text="item"></span> </p> <!--The infinite-loading component should be placed at the bottom of the list, inside the scrolling box! --> <infinite-loading @infinite="infiniteHandler"></infinite-loading> </div></template><script> import InfiniteLoading from 'vue-infinite-loading'; export default { data() { return { list: [] }; }, methods: { infiniteHandler($state) { // Here simulates a loading delay of 1 second setTimeout(() => { let temp = []; for (let i = this.list.length + 1; i <= this.list.length + 20; i++) { temp.push(i); } this.list = this.list.concat(temp); $state.loaded(); }, 1000); }, }, components: { InfiniteLoading } }</script> 2. Paging usage <template> <div> <ul> <li class="hacker-news-item" v-for="(item, key) in list"></li> </ul> <infinite-loading @infinite="infiniteHandler"> No more Data </infinite-loading> </div> </template> <script> import InfiniteLoading from 'vue-infinite-loading'; import axios from 'axios'; export default { data() { return { list: [] }; }, methods: { infiniteHandler($state) { let api="http://xxx.com/xxx"; //api requests data address for you axios.get(api, { params: { // Page number parameters (10 per page) page: this.list.length / 10 + 1, }, }).then((response) => { if (response.data.length) { // response.data is the array list returned by your request interface this.list = this.list.concat(response.data); $state.loaded(); if (this.list.length / 10 === 10) { // Here 10 pages of data are loaded, and the setting is not to load anymore $state.complete(); } } else { $state.complete(); } }); } }, components: { InfiniteLoading } }; </script> Description: $state: This component will pass a special event parameter $state to the event handler to change the loading state. The $state parameter includes three methods, namely loaded method, complete method and reset method.
3. Conditional Usage <template> <div class="hacker-news-list"> <div class="hacker-news-header"> <!--Pull down to change--> <select v-model="tag" @change="changeFilter()"> <option value="story">Story</option> <option value="history">History</option> </select> <!--Or click to change--> <button @click="changeFilter()">Search</button> </div> <ul> <li class="hacker-news-item" v-for="(item, key) in list"></li> </ul> <!--Don't forget to set this ref="infiniteLoading"--> <infinite-loading @infinite="infiniteHandler" ref="infiniteLoading"> No more data </infinite-loading> </div> </template> <script> import InfiniteLoading from 'vue-infinite-loading'; import axios from 'axios'; export default { data() { return { list: [], tag: 'story', }; }, methods: { infiniteHandler($state) { const api="http://xxx.com/xxx"; //api requests data address for you axios.get(api, { params: { // Changed conditional parameters tags: this.tag, page: this.list.length / 10 + 1, }, }).then(({ data }) => { if (data.result.length) { this.list = this.list.concat(data.result); $state.loaded(); if (this.list.length / 20 === 10) { state.complete(); } } else { $state.complete(); } }); }, //Change the conditional bar using this method changeFilter() { this.list = []; this.$nextTick(() => { this.$refs.infiniteLoading.$emit('$InfiniteLoading:reset'); }); }, }, components: { InfiniteLoading } } </script> Official link: https://peachscript.github.io/vue-infinite-loading/ GitHub link: https://github.com/PeachScript/vue-infinite-loading The above is the details of the example of Vue's drop-down scrolling to load data. For more information about Vue's drop-down scrolling to load data, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Examples of using the or statement in MySQL
>>: VMware Workstation 14 Pro installation and activation graphic tutorial
Table of contents 1. Baidu Map API Access 2. Usin...
Table of contents 1. Replace the apply method, ge...
Table of contents Overview Subqueries Subquery Cl...
Table of contents 1. Node builds HTTP server 2. H...
The "nofollow" tag was proposed by Goog...
Table of contents 1. Installation preparation 1. ...
background A colleague is working on his security...
Arrow function is a new feature in ES6. It does n...
There are two types of MySQL installation files, ...
Table of contents Preface 1. Conventional Vue com...
Record the installation and use of openssh-server...
Prepare Environmental information for this articl...
Vue parent component calls the function of the ch...
One line command docker run -d \ -v /share:/home/...
Preface I recently encountered this requirement a...