vue-infinite-loading2.0 usage instructions
This time I will bring you the instructions for using vue-infinite-loading2.0, what are the precautions when using vue-infinite-loading2.0, the following is a practical case, let's take a look.
Introduction
This is an infinite scroll plug-in used in Vue.js, which can help you quickly create an infinite scroll list.
Features
Mobile friendly support
Compatible with any scrollable element
There are different rotators that can be used as loading animations
Supports displaying results after loading
Supports two Unlimited loading in all directions
Installation
Note : vue-infinite-loading2.0 can only be used in Vue.js2.0. If you want to use it in Vue.js1.0, please install vue-infinite-loading1.3 version
npm install vue-infinite-loading --save
Import method
es6 module import method
import InfiniteLoading from 'vue-infinite-loading'; export default { components: { InfiniteLoading, }, };
CommonJS module import method
const InfiniteLoading = require('vue-infinite-loading'); export default { components: { InfiniteLoading, }, };
Other methods
<script src="/path/to/vue-infinite-loading/dist/vue-infinite-loading.js"></script>
vue-infinite-loading.js will be registered A global variable VueInfiniteLoading needs to be used like this:
... components: { VueInfiniteLoading:VueInfiniteLoading.default, } ...
Start
Basic use
In this example, we To create a basic infinite list, there are three steps:
In your template, use v-for to create a list
Place the InfiniteLoading component at the bottom of the list;
Set the ref attribute of the InfiniteLoading component to infiniteLoading because it is used to trigger events.
Create and bind a loading callback function to the InfiniteLoading component.
##Template
<template> <p> <p v-for="item in list"> Line: <span v-text="item"></span> </p> <infinite-loading :on-infinite="onInfinite" ref="infiniteLoading"> </infinite-loading> </p> </template>
Script
import InfiniteLoading from 'vue-infinite-loading'; export default { data() { return { list: [] }; }, methods: { onInfinite() { setTimeout(() => { const temp = []; for (let i = this.list.length + 1; i <= this.list.length + 20; i++) { temp.push(i); } this.list = this.list.concat(temp); this.$refs.infiniteLoading.$emit('$InfiniteLoading:loaded'); }, 1000); } }, components: { InfiniteLoading } };
Example: Hacker News List Page
In this example, we will imitate a Hacker News List page, but will use InfiniteLoading instead of paginationBefore starting this example, we need to prepare the following:- Get the news list API, in this case we use HN Search API
- Import axios plug-in to request data
Template
<p class="hacker-news-list"> <p class="hacker-news-header"> <a target="_blank" href="http://www.ycombinator.com/" rel="external nofollow" rel="external nofollow" >  </a> <span>Hacker News</span> </p> <p class="hacker-news-item" v-for="(item, key) in list"> <span class="num" v-text="key + 1"></span> <p> <a target="_blank" :href="item.url" rel="external nofollow" rel="external nofollow" v-text="item.title"></a> </p> <p> <small> <span v-text="item.points"></span> points by <a target="_blank" :href="'https://news.ycombinator.com/user?id=' + item.author" rel="external nofollow" rel="external nofollow" v-text="item.author"></a> | <a target="_blank" :href="'https://news.ycombinator.com/item?id=' + item.objectID" rel="external nofollow" rel="external nofollow" v-text="item.num_comments + ' comments'"></a> </small> </p> </p> <infinite-loading :on-infinite="onInfinite" ref="infiniteLoading"> <span slot="no-more"> There is no more Hacker News :( </span> </infinite-loading> </p>
Script
import InfiniteLoading from 'vue-infinite-loading'; import axios from 'axios'; const api = 'http://hn.algolia.com/api/v1/search_by_date?tags=story'; export default { data() { return { list: [] }; }, methods: { onInfinite() { axios.get(api, { params: { page: this.list.length / 20 + 1 } }).then((res) => { if (res.data.hits.length) { this.list = this.list.concat(res.data.hits); this.$refs.infiniteLoading.$emit('$InfiniteLoading:loaded'); if (this.list.length / 20 === 3) { this.$refs.infiniteLoading.$emit('$InfiniteLoading:complete'); } } else { this.$refs.infiniteLoading.$emit('$InfiniteLoading:complete'); } }); } }, components: { InfiniteLoading } };
Style
.hacker-news-list .hacker-news-item { margin: 10px 0; padding: 0 10px 0 32px; line-height: 16px; font-size: 14px; } .hacker-news-list .hacker-news-item .num { margin-top: 1px; margin-left: -32px; float: left; width: 32px; color: #888; text-align: right; } .hacker-news-list .hacker-news-item p { padding-left: 8px; margin: 0; } .hacker-news-list .hacker-news-item .num:after { content: "."; } .hacker-news-list .hacker-news-item p>a { color: #333; padding-right: 5px; } .hacker-news-list .hacker-news-item p a { text-decoration: none; } .hacker-news-list .hacker-news-item p small, .hacker-news-list .hacker-news-item p small a { color: #888; }
Use with filter
在上个例子的基础上,我们将在头部创建一个下拉选择作为过滤器,当我们改变过滤器,列表将会重新加载。
Template
<p class="hacker-news-list"> <p class="hacker-news-header"> <a target="_blank" href="http://www.ycombinator.com/" rel="external nofollow" rel="external nofollow" >  </a> <span>Hacker News</span> <select v-model="tag" @change="changeFilter()"> <option value="story">Story</option> <option value="poll">Poll</option> <option value="show_hn">Show hn</option> <option value="ask_hn">Ask hn</option> <option value="front_page">Front page</option> </select> </p> <p class="hacker-news-item" v-for="(item, key) in list"> <span class="num" v-text="key + 1"></span> <p> <a target="_blank" :href="item.url" rel="external nofollow" rel="external nofollow" v-text="item.title"></a> </p> <p> <small> <span v-text="item.points"></span> points by <a target="_blank" :href="'https://news.ycombinator.com/user?id=' + item.author" rel="external nofollow" rel="external nofollow" v-text="item.author"></a> | <a target="_blank" :href="'https://news.ycombinator.com/item?id=' + item.objectID" rel="external nofollow" rel="external nofollow" v-text="item.num_comments + ' comments'"></a> </small> </p> </p> <infinite-loading :on-infinite="onInfinite" ref="infiniteLoading"> <span slot="no-more"> There is no more Hacker News :( </span> </infinite-loading> </p>
Script
import InfiniteLoading from 'vue-infinite-loading'; import axios from 'axios'; const api = 'http://hn.algolia.com/api/v1/search_by_date'; export default { data() { return { list: [], tag: 'story' }; }, methods: { onInfinite() { axios.get(api, { params: { tags: this.tag, page: this.list.length / 20 + 1 } }).then((res) => { if (res.data.hits.length) { this.list = this.list.concat(res.data.hits); this.$refs.infiniteLoading.$emit('$InfiniteLoading:loaded'); if (this.list.length / 20 === 10) { this.$refs.infiniteLoading.$emit('$InfiniteLoading:complete'); } } else { this.$refs.infiniteLoading.$emit('$InfiniteLoading:complete'); } }); }, changeFilter() { this.list = []; this.$nextTick(() => { this.$refs.infiniteLoading.$emit('$InfiniteLoading:reset'); }); } }, components: { InfiniteLoading } };
在changeFilter函数中,我们清楚了列表并等待DOM更新,然后我们触发一个$InfiniteLoading:reset事件,目的是让 InfiniteLoading 组件回到最初状态,它将立刻请求新的数据。
Style
在上个例子基础上增加样式
.demo-inner { margin-left: 20px; width: 261px; height: 455px; border: 1px solid #ccc; overflow: auto; } .hacker-news-list .hacker-news-header { padding: 2px; line-height: 14px; background-color: #f60; } .hacker-news-list { min-height: 455px; background-color: #f6f6ef; } .hacker-news-list .hacker-news-header select { float: right; color: #fff; background-color: transparent; border: 1px solid #fff; outline: none; }
服务端渲染
服务端渲染(SSR)是Vue.js2.0的新特性,当你在你的SSR应用中使用这个组件,会得到类似这样的错误:
Error: window is not defined ReferenceError: window is not defined at ... at ... at e.exports (...) at Object. (...) at p (...) at Object.e.exports.render.e (...) at p (...) at Object. (...) at p (...) at e.esModule.default (...)
因为style-loader不支持在这个时候本地导出,详情点这里,所以我们需要下面的变通方案,为了你的SSR应用:
import InfiniteLoading from 'vue-infinite-loading/src/components/Infiniteloading.vue';
代替
import InfiniteLoading from 'vue-infinite-loading';
npm install less less-loader --save-dev 如果你还没有安装它们。
然后你的SSR应用应该运行良好。如果不是,你可以加入这个issue去讨论。
属性
on-infinite
这是一个回调函数,当滚动到距离滚动父元素底部特定距离的时候,会被调用。
通常,在数据加载完成后,你应该在这个函数中发送$InfiniteLoading:loaded事件。
- type Function - reuqired true
distance
这是滚动的临界值。如果到滚动父元素的底部距离小于这个值,那么on-infinite回调函数就会被调用。
- type Number - required false - default 100 - unit pixel
spinner
通过这个属性,你可以选择一个你最喜爱旋转器作为加载动画。点击这里可以看到所有可用的旋转器。
- type String - required false - default 'default'
ref
正如你所知,这个属性是一个Vue.js的官方指令,用来获取子组件的实例。我们需要用它来得到 InfiniteLoading 组件的实例来发送事件。你可以用这种方式来得到实例:this.$refs[the value of ref attribute].
- type String - required true
direction
如果你设置这个属性为top,那么这个组件将在你滚到顶部的时候,调用on-infinite函数。
警告:你必须在数据加载后,手动地将滚动父元素的scrollTop设置为正确的值,否则,该组件会一次又一次调用on-infinite函数。
- type String - default 'bottom'
事件
InfiniteLoading 组件将处理一下事件。如果你需要通过组件的实例来$emit,则可以通过ref属性来得到组件实例。
$InfiniteLoading:loaded
通常,你需要在数据加载后发送这个事件, InfiniteLoading组件将隐藏加载动画,并且准备下一次触发。
$InfiniteLoading:complete
如果InfiniteLoading组件就不会接收$InfiniteLoading:loaded,当你发送这个事件后,它将为用户显示一个没有结果的提示。如果InfiniteLoading组件接收过$InfiniteLoading:loaded,当你发送这个事件的时候,它会为用户显示一个没有更多内容的提示。你可以利用slot来自定义需要显示的内容。
你的onInfinite函数可能像这个样子:
onInfinite() { this.$http.get(url, (res) => { if (res.data) { this.list = this.list.concat(res.data); this.$refs[your ref attirbute's value].$emit('$InfiniteLoading:loaded'); } else { this.$refs[your ref attirbute's value].$emit('$InfiniteLoading:complete'); } }); }
$InfiniteLoading:reset
InfiniteLoading组件将会回到最初的状态,并且on-infinite函数将会立刻被调用。大部分情况下,如果你把这个组件同过滤器或制表符一起使用,这个事件还是有用的。
插槽
你可以利用slot自定义提示的内容,当然,如果你喜欢的话,也可以使用默认内容:
<span slot="{{ slot name }}"> {{ Your content }} </span>
no-results
当InfiniteLoading组件接收到$InfiniteLoading:complete 事件并且它没有接收过$InfiniteLoading:loaded事件时,这个内容会显示出来。
- type String - default No results :(
no-more
当InfiniteLoading组件接收到$InfiniteLoading:complete 事件并且它已经接收过$InfiniteLoading:loaded事件时,这个内容会出现。
spinner
如果,你不喜欢当前旋转器,你可以自定义自己的旋转器作为加载时的动画。
- type HTML - default default spinner
旋转器
你可以用spinner属性,选择你最喜爱的旋转器作为加载动画:
<infinite-loading spinner="{{ spinner name }}"></infinite-loading>
点击这里可以查看几个可用的旋转器。
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
Vue无限加载vue-infinite-loading使用详解
The above is the detailed content of vue-infinite-loading2.0 usage instructions. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. Switch between noise reduction mode and transparency mode. Press and hold the handle of the earphones for about 1 second to switch between noise reduction mode and transparency mode. 2. In music mode, press the earphone handle once to pause or play music. Press the earphone handle twice to play the next song. Press the earphone handle three times to play the previous song or wake up the voice. 3. In call mode, during a call, press the earphone handle once to answer or hang up the call. 4. How to reset Open the earphone box. When the charging box indicator light flashes red 5 times, release the button and the earphones are reset. 3. How to connect the phone 1. Open the charging box 2. Press and hold the setting button for 2 seconds 3. When a pop-up window appears on the phone screen, click to confirm the connection. 4. How to check the battery status 1. When the earphones are connected to the mobile phone, you can check the battery level of the earphones and charging box in the pop-up window on the mobile phone screen. 2,

Instructions for accessing and using the payment function of UniApp. With the popularity of mobile payment, many applications need to integrate payment functions to facilitate users to make online payments. As a cross-platform development framework based on Vue.js, UniApp has the characteristics of one-time development and multi-platform use, and can easily implement the payment function. This article will introduce how to access the payment function in UniApp and give code examples. 1. To access the payment function, add payment permissions in the manifest.json file on the App side:

How to use the Hyperf framework for multi-language processing Introduction: With the globalization of the Internet, multi-language processing has become an essential skill for the development of many applications. In web application development, it is very important to be able to support multiple languages because it can help you better meet the needs of different users. This article will introduce how to use the Hyperf framework for multi-language processing and provide specific code examples. Install the Hyperf framework First, we need to install the Hyperf framework. You can use the composer command to

How to use the video recording function in uniapp Today, the author will introduce to you how to use the video recording function in the uniapp development framework. uniapp is a cross-platform development framework. We can run our applications on multiple platforms at the same time based on the code written once, which is very convenient for developers. In uniapp, we can use the uni-AD-IN camera component to implement the video recording function. First, we need to install uni-

Cookies are a common web technology used to store information about users' personal preferences and behavior on websites. In today's digital age, almost all websites use cookies to provide personalization and a better user experience. This article will introduce the use of cookies in detail to help users better understand and master this technology. First, let's understand the basic concept of cookies. Cookies are small text files stored on the user's browser by the website and contain information about the user's visit to the website.

How to filter data using advanced filtering In daily work, filtering large amounts of data is a very common and important task. The conventional filtering function may not be able to meet the needs for more precise and complex filtering of data. To solve this problem, many office software provide advanced filtering functions that can help users filter data more efficiently. This article explains how to use advanced filtering features to filter data. Step 1: Prepare data Before using the advanced filtering function, you first need to prepare the data to be filtered. Make sure the data is complete

The win7 system, like other windows systems, needs to be activated to use all functions. So how to activate win7? The commonly used method is to use win7 activation code or win7 activation tool, and the easier to use is the win7activation activation tool. The editor below will introduce to you how to use the win7activation activation tool. The specific method is as follows: 1. First download "WIN7Activation" (Win7 activation tool) online, copy the program to your computer, and double-click it to open it. 2. Then click directly: Activate, and the activation state will automatically begin. 3. After the program activates the win7 system, a prompt pops up, click: Yes. 4. Restart

How to use the Zizai Zhao app? This software not only helps users quickly find lost things, but also has a dedicated smart reminder function that allows you to quickly bring everything with you. We should use such easy-to-use software. For users who have just downloaded it, how should we operate this software if we want to quickly retrieve things? Let us take a look at the operation method of this software. I hope it can effectively help you quickly retrieve goods. Tutorial on how to use the Zizai Zhao app software. 1. Download the nut Zizai Zhao app software provided by this site, turn on Bluetooth, and your mobile phone must support it. Bluetooth 4.0 2. Open the software and register a new user. You can choose to register via mobile phone, or log in via Weibo or QQ. 3. Will
