Infinity Loading – Load infinity with Vue.js be like facebook!

Tram Ho

Preface

Hello friends. If you have read through my Vue.js series, you should have grasped the basics of this framework pretty much. No more long and roundabout. Today I continue to share a UI set to use with Vue.js with a very simple function and implemented with Vue.js which is Loading Infinity.  Something that people encounter everyday when using facebook or on social networking sites. At first, I thought this loading style was very magical, but when I try to refresh it is very easy [email protected] !

Let’s go to the post

1. Things you need

  • Element: This article I will use and introduce to you a very famous ui set and commonly used with Vue.js which is Element UI. Although I have not used all the tools in this UI set, but at first glance I see this UI set is quite beautiful as well as very professional.

This interface set is almost full of things that we need to work with a lot of supportive Components. Of course if there is a need to do something special, we also have to code by hand.

The latest version is currently 2.13.2. You look to the left sidebar of the Component tab, you will see a lot of Components available such as DatePicker, Icons, Buttons, Links, … With our self-learning spirit, I think everything in this should go to the homepage. to explore and explore is the best !! https://element.eleme.io/

  • and VueJS: As a topic, VueJS will be an indispensable part of this series. Here if you have never used VueJS can go back to the previous articles in the series to read the basics and come back to practice (many thanks!)

2. And try enough

Let’s go to installation and let’s do it.

If you are working on a project and just want to add functionality, skip this installation step

Setting :

Step 1: I will use VueCLI to create a new project with Vue. Everything about the CLI installation is invited once again to check out my previous posts !!

Step 2: Next, we will install Element into Vue by command:

import into the main.js file for use with Vue

Step 3: CODE

In components / List.vue I will execute the component code

The main template I use Element is only for directive v-infinite-scroll and infinite-scroll-disabled. Also you can install v-infinite-scroll separately without using Element via npm (using Element here just wants to introduce you to this set of quite useful tools: v)

Script part:

The above code is just the most basic things that we have read about in the previous articles plus a fetch method to get data from api. As I see, usually loading data will not load all to perform this loading infinity function, but instead we will apply to load paged data and load each page there (Lazy Loading). If you are interested and do not know how to do this, leave a comment below. Maybe if I’m free, I will have a post about lazy loading

Here the algorithm is also quite simple, first I load all the data from the api, we will initialize the first time without scrolling the mouse with the initial data (init) of 3 records (using splice and save in the state dataToShow Next each time we scroll the mouse, we will continue to split the original data loaded and retrieve 3 more records to join this dataToShow state.And we just keep separating the data until the state data is not. For more data, we will stop and change state loading = false as well as change state noMore to true. So we have a complete infinity loading function.

Achievement:

3. Conclusion

With this article, my main purpose is to introduce to you a set of delicious and inexpensive tools that are Element as well as the application to do a simple function. Hope to help newcomers to better grasp their knowledge to start working with the Vue.js. framework. See you all in the following articles. Thank you for watching the series and to the end of this post !!!

Share the news now

Source : Viblo