Home  >  Article  >  Web Front-end  >  How to implement infinite loading of data by scrolling to the bottom of the page under Vue

How to implement infinite loading of data by scrolling to the bottom of the page under Vue

php中世界最好的语言
php中世界最好的语言Original
2018-06-02 13:59:423431browse

This time I will show you how to implement infinite loading of data by scrolling to the bottom of the page under Vue, and how to infinitely load data by scrolling to the bottom of the page under Vue. one time. I saw an article Implementing an Infinite Scroll with Vue.js and thought it was quite practical, so I read it and briefly translated it for reference for those who need it.

From this project you can To deepen our understanding of Vue's

life cycle

, when to start axios request, how to use native js with Vue to write scroll events, etc. Here I mainly extract and supplement the key points of the original text

Technical points of this article

    Vue life cycle
  1. axios simple usage
  2. moment.js format date
  3. Lazy loading of pictures
  4. Combined with native js to write scroll events
  5. Request throttling

Create projectFirst create a simple vue project

# vue init webpack-simple infinite-scroll-vuejs

Then install some plug-ins needed for the project

# npm install axios moment

Initialize user data

After the project is built, run it and take a look

# npm run dev

After opening http://localhost:8080 correctly, we started to modify the code. Let's first look at obtaining user data.

The original author also specifically reminded here that it is completely unnecessary and not recommended when loading the page. Requested 5 times, but this interface can only return 1 piece of data at a time, and it is only used for testing. Of course, I can also simulate data through Mock, so I won’t go into details~

Next Write the template structure and style as follows:


This way the page can display the personal information of 5 people.

Processing infinite scroll loading logic

We next need to add scroll() in methods to monitor scrolling, and this event should be within the life cycle of mounted(). The code is as follows:

The original text of this code has some spelling errors. I have corrected it here, and also increased the distance from the bottom to start loading data and intercepting the flow.

Save it, go back to the browser, and check the effect. There is no problem anymore~

Summary

The function of scrolling to the bottom of the page and infinite loading is implemented on Vue. In fact, it is similar to ordinary page development. Each time the scrolling load is not completed, the next request will not be triggered. Each request Push into the array, and implement lazy loading through

data binding

of (actually 0 0, I don't really agree with it...), after reading it, it seems quite simple. .Finally, I also made a copy of this on GitHub. If you need it, you can take a look at infinite-scroll-vuejs-demo~

I believe you have mastered it after reading the case in this article. For more exciting methods, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use JS to implement Huffman coding


How to use Angular CLI to create an Angular project

The above is the detailed content of How to implement infinite loading of data by scrolling to the bottom of the page under Vue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn