Home > Article > Web Front-end > vue-infinite-loading2.0 Chinese documentation detailed explanation
This article mainly introduces the detailed explanation of vue-infinite-loading2.0 Chinese documentation. Now I share it with you and give it as a reference.
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
9edcdb420db994b8fec71e3b24005d92Installation94b3e26ee717c64999d7867364b1b4a3
8e99a69fbe029cd4e2b854e244eab143Note : 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 version128dba7a3a77be0113eb0bea6ea0a5d0
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 ways
<script src="/path/to/vue-infinite-loading/dist/vue-infinite-loading.js"></script>
vue-infinite-loading.js will register a global The variable VueInfiniteLoading needs to be used like this:
... components: { VueInfiniteLoading:VueInfiniteLoading.default, } ...
Start
Basic use
In this example, we will create a basic infinite list, with the following 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 } };
In the 8e99a69fbe029cd4e2b854e244eab143onInfinite128dba7a3a77be0113eb0bea6ea0a5d0 function, each time we push 20 numbers into the list array. We use 8e99a69fbe029cd4e2b854e244eab143setTimeout128dba7a3a77be0113eb0bea6ea0a5d0 to simulate asynchronous requests. Finally, don't forget to trigger an 8e99a69fbe029cd4e2b854e244eab143$InfiniteLoading:loaded128dba7a3a77be0113eb0bea6ea0a5d0 event, which will tell the 8e99a69fbe029cd4e2b854e244eab143InfiniteLoading128dba7a3a77be0113eb0bea6ea0a5d0 component that the data has been downloaded successfully.
Now, we can show the effect based on the above code.
679471b55d041765674c739a56d9a112Example: Hacker News List Page94b3e26ee717c64999d7867364b1b4a3
In this example, we will imitate a Hacker News List page, but will use 8e99a69fbe029cd4e2b854e244eab143InfiniteLoading128dba7a3a77be0113eb0bea6ea0a5d0 instead of 8e99a69fbe029cd4e2b854e244eab143pagination128dba7a3a77be0113eb0bea6ea0a5d0
Before 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" > ![](https://news.ycombinator.com/y18.gif) </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>
In the template, we created a header and a list for the Hacker News list. The 8e99a69fbe029cd4e2b854e244eab143InfiniteLoading128dba7a3a77be0113eb0bea6ea0a5d0 component in this example is used somewhat differently from the previous example. We customized the prompt content when there is no more data based on 8e99a69fbe029cd4e2b854e244eab143slot128dba7a3a77be0113eb0bea6ea0a5d0.
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 } };
In the 8e99a69fbe029cd4e2b854e244eab143onInfinite128dba7a3a77be0113eb0bea6ea0a5d0 function, we request a page news, and push them into the list array each time. If we request 3 pages of news, the 8e99a69fbe029cd4e2b854e244eab143$InfiniteLoading:complete128dba7a3a77be0113eb0bea6ea0a5d0 event will be triggered to tell the 8e99a69fbe029cd4e2b854e244eab143InfiniteLoading128dba7a3a77be0113eb0bea6ea0a5d0 component that there is no more data to load. It will display the prompt content we customized in the template to indicate that there is no more data.
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; }
##f6aae858ab9e7ef879739ead0f8fb1a3Use with filter 94b3e26ee717c64999d7867364b1b4a3
Based on the previous example, we will create a drop-down selection in the header as a filter. When we change the filter, the list will reload.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" > ![](https://news.ycombinator.com/y18.gif) </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 } };
In the 8e99a69fbe029cd4e2b854e244eab143changeFilter128dba7a3a77be0113eb0bea6ea0a5d0 function, we clear the list and wait for the DOM to update, then we trigger an 8e99a69fbe029cd4e2b854e244eab143$InfiniteLoading:reset128dba7a3a77be0113eb0bea6ea0a5d0 event for the purpose Return the 8e99a69fbe029cd4e2b854e244eab143 InfiniteLoading 128dba7a3a77be0113eb0bea6ea0a5d0 component to its original state and it will immediately request new data.
StyleAdd a style based on the previous example
.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; }
##< ;p id="server">Server rendering94b3e26ee717c64999d7867364b1b4a3
服务端渲染(SSR)是8e99a69fbe029cd4e2b854e244eab143Vue.js2.0128dba7a3a77be0113eb0bea6ea0a5d0的新特性,当你在你的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 (...)
因为8e99a69fbe029cd4e2b854e244eab143style-loader128dba7a3a77be0113eb0bea6ea0a5d0不支持在这个时候本地导出,详情点这里,所以我们需要下面的变通方案,为了你的SSR应用:
import InfiniteLoading from 'vue-infinite-loading/src/components/Infiniteloading.vue';
代替
import InfiniteLoading from 'vue-infinite-loading';
8e99a69fbe029cd4e2b854e244eab143npm install less less-loader --save-dev128dba7a3a77be0113eb0bea6ea0a5d0 如果你还没有安装它们。
然后你的SSR应用应该运行良好。如果不是,你可以加入这个issue去讨论。
74f6db73f95d6c7cf113470b60f859ff属性e388a4556c0f65e1904146cc1a846bee
on-infinite
这是一个回调函数,当滚动到距离滚动父元素底部特定距离的时候,会被调用。
通常,在数据加载完成后,你应该在这个函数中发送8e99a69fbe029cd4e2b854e244eab143$InfiniteLoading:loaded128dba7a3a77be0113eb0bea6ea0a5d0事件。
- type Function - reuqired true
distance
这是滚动的临界值。如果到滚动父元素的底部距离小于这个值,那么8e99a69fbe029cd4e2b854e244eab143on-infinite128dba7a3a77be0113eb0bea6ea0a5d0回调函数就会被调用。
- type Number - required false - default 100 - unit pixel
spinner
通过这个属性,你可以选择一个你最喜爱旋转器作为加载动画。点击这里可以看到所有可用的旋转器。
- type String - required false - default 'default'
ref
正如你所知,这个属性是一个Vue.js的官方指令,用来获取子组件的实例。我们需要用它来得到8e99a69fbe029cd4e2b854e244eab143 InfiniteLoading 128dba7a3a77be0113eb0bea6ea0a5d0组件的实例来发送事件。你可以用这种方式来得到实例:8e99a69fbe029cd4e2b854e244eab143this.$refs[the value of ref attribute].128dba7a3a77be0113eb0bea6ea0a5d0
- type String - required true
direction
如果你设置这个属性为top,那么这个组件将在你滚到顶部的时候,调用on-infinite函数。
8e99a69fbe029cd4e2b854e244eab143警告:你必须在数据加载后,手动地将滚动父元素的scrollTop设置为正确的值,否则,该组件会一次又一次调用on-infinite函数。128dba7a3a77be0113eb0bea6ea0a5d0
- type String - default 'bottom'
38c38741122f26a80e2405a6b6423450事件94b3e26ee717c64999d7867364b1b4a3
8e99a69fbe029cd4e2b854e244eab143InfiniteLoading 128dba7a3a77be0113eb0bea6ea0a5d0组件将处理一下事件。如果你需要通过组件的实例来8e99a69fbe029cd4e2b854e244eab143$emit128dba7a3a77be0113eb0bea6ea0a5d0,则可以通过8e99a69fbe029cd4e2b854e244eab143ref128dba7a3a77be0113eb0bea6ea0a5d0属性来得到组件实例。
$InfiniteLoading:loaded
通常,你需要在数据加载后发送这个事件,8e99a69fbe029cd4e2b854e244eab143 InfiniteLoading128dba7a3a77be0113eb0bea6ea0a5d0组件将隐藏加载动画,并且准备下一次触发。
$InfiniteLoading:complete
如果8e99a69fbe029cd4e2b854e244eab143InfiniteLoading128dba7a3a77be0113eb0bea6ea0a5d0组件就不会接收8e99a69fbe029cd4e2b854e244eab143$InfiniteLoading:loaded128dba7a3a77be0113eb0bea6ea0a5d0,当你发送这个事件后,它将为用户显示一个没有结果的提示。如果8e99a69fbe029cd4e2b854e244eab143InfiniteLoading128dba7a3a77be0113eb0bea6ea0a5d0组件接收过8e99a69fbe029cd4e2b854e244eab143$InfiniteLoading:loaded128dba7a3a77be0113eb0bea6ea0a5d0,当你发送这个事件的时候,它会为用户显示一个没有更多内容的提示。你可以利用slot来自定义需要显示的内容。
你的8e99a69fbe029cd4e2b854e244eab143onInfinite128dba7a3a77be0113eb0bea6ea0a5d0函数可能像这个样子:
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
8e99a69fbe029cd4e2b854e244eab143InfiniteLoading128dba7a3a77be0113eb0bea6ea0a5d0组件将会回到最初的状态,并且8e99a69fbe029cd4e2b854e244eab143on-infinite128dba7a3a77be0113eb0bea6ea0a5d0函数将会立刻被调用。大部分情况下,如果你把这个组件同过滤器或制表符一起使用,这个事件还是有用的。
92d3604ac803d08d30b463039f74eda2插槽94b3e26ee717c64999d7867364b1b4a3
你可以利用8e99a69fbe029cd4e2b854e244eab143slot128dba7a3a77be0113eb0bea6ea0a5d0自定义提示的内容,当然,如果你喜欢的话,也可以使用默认内容:
<span slot="{{ slot name }}"> {{ Your content }} </span>
no-results
当8e99a69fbe029cd4e2b854e244eab143InfiniteLoading128dba7a3a77be0113eb0bea6ea0a5d0组件接收到8e99a69fbe029cd4e2b854e244eab143$InfiniteLoading:complete 128dba7a3a77be0113eb0bea6ea0a5d0事件并且它没有接收过8e99a69fbe029cd4e2b854e244eab143$InfiniteLoading:loaded128dba7a3a77be0113eb0bea6ea0a5d0事件时,这个内容会显示出来。
- type String - default No results :(
no-more
当8e99a69fbe029cd4e2b854e244eab143InfiniteLoading128dba7a3a77be0113eb0bea6ea0a5d0组件接收到8e99a69fbe029cd4e2b854e244eab143$InfiniteLoading:complete 128dba7a3a77be0113eb0bea6ea0a5d0事件并且它已经接收过8e99a69fbe029cd4e2b854e244eab143$InfiniteLoading:loaded128dba7a3a77be0113eb0bea6ea0a5d0事件时,这个内容会出现。
spinner
如果,你不喜欢当前旋转器,你可以自定义自己的旋转器作为加载时的动画。
- type HTML - default default spinner
22f5f78b10ba20c8dd94b11e3de0c231旋转器94b3e26ee717c64999d7867364b1b4a3
你可以用8e99a69fbe029cd4e2b854e244eab143spinner128dba7a3a77be0113eb0bea6ea0a5d0属性,选择你最喜爱的旋转器作为加载动画:
<infinite-loading spinner="{{ spinner name }}"></infinite-loading>
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
jquery中的ajax如何返回结果而非回调方式即为同顺序执行
The above is the detailed content of vue-infinite-loading2.0 Chinese documentation detailed explanation. For more information, please follow other related articles on the PHP Chinese website!