npm install vue-infinite-loading --save |
import InfiniteLoading from 'vue-infinite-loading' ; export default { components: { InfiniteLoading } } |
1.基本用法
?<template> <div> <p v- for = "item in list" > <span v-text= "item" ></span> </p> <!--infinite-loading这个组件要放在列表的底部,滚动的盒子里面!--> <infinite-loading @infinite= "infiniteHandler" ></infinite-loading> </div></template><script> import InfiniteLoading from 'vue-infinite-loading' ; export default { data() { return { list: [] }; }, methods: { infiniteHandler($state) { // 这里模仿加载延迟1秒钟 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.分页用法
?<template> <div> <ul> <li class= "hacker-news-item" v- for = "(item, key) in list" ></li> </ul> <infinite-loading @infinite= "infiniteHandler" > <span slot= "no-more" >No more Data</span> </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为你请求数据地址 axios.get(api, { params: { // 页码参数(10条每页) page: this .list.length / 10 + 1, }, }).then((response) => { if (response.data.length) { // response.data为你请求接口返回的数组列表 this .list = this .list.concat(response.data); $state.loaded(); if ( this .list.length / 10 === 10) { // 这里是加载了10页数据,设置不在加载 $state.complete(); } } else { $state.complete(); } }); } }, components: { InfiniteLoading } }; </script> |
说明: $state: 该组件会传递一个特殊的事件参数$state给事件处理器来改变加载状态,$state参数包括三个方法,它们是loaded方法,complete方法和reset方法。
3.条件用法
?<template> <div class= "hacker-news-list" > <div class= "hacker-news-header" > <!--下拉改变--> <select v-model= "tag" @change= "changeFilter()" > <option value= "story" >Story</option> <option value= "history" >History</option> </select> <!--或者点击改变--> <button @click= "changeFilter()" >搜索</button> </div> <ul> <li class= "hacker-news-item" v- for = "(item, key) in list" ></li> </ul> <!--不要忘记设置这个 ref= "infiniteLoading" --> <infinite-loading @infinite= "infiniteHandler" ref= "infiniteLoading" > <span slot= "no-more" >No more data</span> </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为你请求数据地址 axios.get(api, { params: { // 改变的条件参数 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(); } }); }, //改变条件条用此方法 changeFilter() { this .list = []; this .$nextTick(() => { this .$refs.infiniteLoading.$emit( '$InfiniteLoading:reset' ); }); }, }, components: { InfiniteLoading } } </script> |
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理