日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > vue >内容正文

vue

Vue使用vue-pull-refresh插件实现下拉刷新

發(fā)布時(shí)間:2025/3/19 vue 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Vue使用vue-pull-refresh插件实现下拉刷新 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

效果

vue-pull-refresh插件

github地址:

https://github.com/lakb248/vue-pull-refresh

在線Demo演示:

https://lakb248.github.io/vue-pull-refresh/

實(shí)現(xiàn)

安裝插件

在項(xiàng)目目錄下打開cmd,輸入:

npm install --save vue-pull-refresh

實(shí)現(xiàn)刷新

打開要實(shí)現(xiàn)下拉刷新的界面,這里是morelist.vue,更多頁面,實(shí)現(xiàn)下拉刷新歌曲列表。

1.首先在頁面引入插件

import VuePullRefresh from 'vue-pull-refresh';

2.然后聲明插件

components:{VuePullRefresh},

3.將要下拉的頁面代碼用<VuePullRefresh>嵌套

? <VuePullRefresh :on-refresh="onRefresh"><div class="info url log" v-for="(item,index) in moreListData" :key="index"><div class="poster"><img :src="item.pic_big" :alt="item.title"></div><div class="text-wrap"><div class="title">{{ item.title }}</div><div class="author">{{ item.artist_name }}</div></div></div></VuePullRefresh>

其中:on-refresh="onRefresh" 表示下拉時(shí)要執(zhí)行的方法

4.新建下拉時(shí)執(zhí)行的方法

methods:{// 下拉的時(shí)候觸發(fā)函數(shù)onRefresh: function() {var that = this;const moreListUrl = this.HOST +"/v1/restserver/ting?method=baidu.ting.billboard.billList&type= "+ this.$route.params.musictype+"&size=12&offset="+this.offset;return new Promise(function (resolve, reject) {setTimeout(() => {that.$axios.get(moreListUrl).then(res => {console.log(res.data);that.offset >= res.data.billboard.billboard_songnum - 12 ? console.log("沒數(shù)據(jù)了") : that.offset += 12,// that.moreListData = that.moreListData.concat(res.data.song_list)that.moreListData = res.data.song_listresolve();}).catch(error => {console.log(error);})})});}}

注:

此方法是從百度音樂接口獲取音樂數(shù)據(jù),其中offset是偏移量。默認(rèn)是0.

在mounted鉤子函數(shù)中,會(huì)首先從接口中獲取12條數(shù)據(jù),然后將偏移量offset加12.

在刷新方法中,會(huì)重新請求接口數(shù)據(jù),此時(shí)的便宜量參數(shù)以及加了12,所以歌曲的數(shù)據(jù)會(huì)發(fā)生改變,然后通過選擇表達(dá)式判斷偏移量是否大于接口返回?cái)?shù)據(jù)中音樂的總數(shù)量,從而進(jìn)行相應(yīng)的判斷,是則沒有更多數(shù)據(jù)偏移量不會(huì)再增加,那么再刷新也會(huì)請求相同的數(shù)據(jù),無法再刷新。否則偏移量繼續(xù)加12,請求的數(shù)據(jù)不同,從而實(shí)現(xiàn)數(shù)據(jù)刷新。

完整morelist.vue代碼

<template lang="html"><div class="more-list"><div class="wrapper"><h3>{{ this.$route.params.title }}</h3><VuePullRefresh :on-refresh="onRefresh"><div class="info url log" v-for="(item,index) in moreListData" :key="index"><div class="poster"><img :src="item.pic_big" :alt="item.title"></div><div class="text-wrap"><div class="title">{{ item.title }}</div><div class="author">{{ item.artist_name }}</div></div></div></VuePullRefresh></div></div> </template><script>import VuePullRefresh from 'vue-pull-refresh';export default {name:"morelist",data(){return{moreListData:[],offset:0}},components:{VuePullRefresh},mounted(){const moreListUrl = this.HOST + "/v1/restserver/ting?method=baidu.ting.billboard.billList&type="+ this.$route.params.musictype +"&size=12&offset=0"this.$axios.get(moreListUrl).then(res => {this.moreListData = res.data.song_listthis.offset = this.offset+12}).catch(error => {console.log(error);})},methods:{// 下拉的時(shí)候觸發(fā)函數(shù)onRefresh: function() {var that = this;const moreListUrl = this.HOST + "/v1/restserver/ting?method=baidu.ting.billboard.billList&type="+ this.$route.params.musictype +"&size=12&offset="+this.offset;return new Promise(function (resolve, reject) {setTimeout(() => {that.$axios.get(moreListUrl).then(res => {console.log(res.data);that.offset >= res.data.billboard.billboard_songnum - 12 ? console.log("沒數(shù)據(jù)了") : that.offset += 12,// that.moreListData = that.moreListData.concat(res.data.song_list)that.moreListData = res.data.song_listresolve();}).catch(error => {console.log(error);})})});}} } </script><style scoped>.wrapper {padding-top: 13px;text-align: center;margin-bottom: 10px;background: #fff;clear: both;overflow: hidden; }h3{font-size: 22px;text-align: left;margin-left: 17px;margin-bottom: 5px; }.wrapper .info {width: 42%;float: left;text-align: center;padding-left: 17px;display: block;text-align: left;margin-bottom: 10px;position: relative; }</style>


?

總結(jié)

以上是生活随笔為你收集整理的Vue使用vue-pull-refresh插件实现下拉刷新的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。