vue.js整合vux中的上拉加载下拉刷新实例教程

作者:朋也 时间:2024-04-30 10:30:17 

前言

Vux 是基于 Vue 和 Weui 开发的手机端页面 UI 组件库,开发初衷是满足公司的微信端表单需求,因为第三方的调查问卷表单系统在手机上实在比较丑(还是 PC 那一套样式适配了大小而已)。于是用 vue 重构了表单组件,后来一发不可收拾把其他常用组件也一并开发了。

相比于 React 还是更喜欢用 Vue ,除了目前社区组件不多,周边构建工具还是比较完善的(作者也特别勤奋)。

下面话不多说了,来一看看详细的介绍吧。

先上图

vue.js整合vux中的上拉加载下拉刷新实例教程

创建项目

使用vue-cli 创建一个vue项目

安装vux,可以参考:vux快速入门

配置

官方文档地址

打开后会看到一段话

该组件已经不再维护,也不建议使用,大部分情况下也不需要用到该组件。 建议使用第三方相关组件,相关 issue 将不会处理。

不知道作者为啥不维护了,明明需求挺多的

我没有用demo里的 LoadMore 组件,用的是 Scroller里自带的 use-pullup, use-pulldown 下面是我的配置


<!--
height: 我用到x-header了,文档里说header高是48px,所以这里设置成-48
-->
<scroller use-pullup :pullup-config="pullupDefaultConfig" @on-pullup-loading="loadMore"
 use-pulldown :pulldown-config="pulldownDefaultConfig" @on-pulldown-loading="refresh"
 lock-x ref="scrollerBottom" height="-48">
</scroller>
<script>
import {Scroller, XHeader} from 'vux'
const pulldownDefaultConfig = {
content: '下拉刷新',
height: 40,
autoRefresh: false,
downContent: '下拉刷新',
upContent: '释放后刷新',
loadingContent: '正在刷新...',
clsPrefix: 'xs-plugin-pulldown-'
}
const pullupDefaultConfig = {
content: '上拉加载更多',
pullUpHeight: 60,
height: 40,
autoRefresh: false,
downContent: '释放后加载',
upContent: '上拉加载更多',
loadingContent: '加载中...',
clsPrefix: 'xs-plugin-pullup-'
}
export default {
components: {
XHeader,
Scroller
},
mounted() {
this.$nextTick(() => {
this.$refs.scrollerBottom.reset({top: 0})
})
},
data() {
return {
list: [],
pullupDefaultConfig: pullupDefaultConfig,
pulldownDefaultConfig: pulldownDefaultConfig
}
},
methods: {
refresh() {
},
loadMore() {

}
}
}
</script>

请求接口遍历数据

接口服务用的是mock.js生成的数据,可以看一下这篇文章:使用mock.js随机数据和使用express输出json接口

安装 axios


yarn add axios

//...
methods: {
fetchData(cb) {
 axios.get('http://localhost:3000/').then(response => {
 this.$nextTick(() => {
  this.$refs.scrollerBottom.reset()
 })
 cb(response.data)
 })
}
}
//...

完善refresh,loadMore方法


//...
methods: {
refresh() {
 this.fetchData(data => {
 this.list = data.list
 this.$refs.scrollerBottom.enablePullup()
 this.$refs.scrollerBottom.donePulldown()
 })
},
loadMore() {
 this.fetchData(data => {
 if (this.list.length >= 10) {
  this.$refs.scrollerBottom.disablePullup()
 }
 this.list = this.list.concat(data.list)
 this.$refs.scrollerBottom.donePullup()
 })
}
}
//...

在组件加载的时候调用一下 loadMore 方法


//...
mounted() {
this.$nextTick(() => {
 this.$refs.scrollerBottom.reset({top: 0})
})
this.loadMore()
}
//...

最后把html部分补全


<scroller>
<div style="padding: 10px 0">
<div class="box" v-for="(item, index) in list" :key="index">
 <p class="list"></p>
</div>
</div>
</scroller>

完整代码


<template>
<div>
<x-header :left-options="{'showBack': false}">上拉加载,下拉刷新</x-header>
<scroller use-pullup :pullup-config="pullupDefaultConfig" @on-pullup-loading="loadMore"
   use-pulldown :pulldown-config="pulldownDefaultConfig" @on-pulldown-loading="refresh"
   lock-x ref="scrollerBottom" height="-48">
 <div style="padding: 10px 0">
 <div class="box" v-for="(item, index) in list" :key="index">
  <p class="list"></p>
 </div>
 </div>
</scroller>
</div>
</template>
<script>
import {Scroller, XHeader} from 'vux'
import axios from 'axios'

const pulldownDefaultConfig = {
content: '下拉刷新',
height: 40,
autoRefresh: false,
downContent: '下拉刷新',
upContent: '释放后刷新',
loadingContent: '正在刷新...',
clsPrefix: 'xs-plugin-pulldown-'
}
const pullupDefaultConfig = {
content: '上拉加载更多',
pullUpHeight: 60,
height: 40,
autoRefresh: false,
downContent: '释放后加载',
upContent: '上拉加载更多',
loadingContent: '加载中...',
clsPrefix: 'xs-plugin-pullup-'
}
export default {
components: {
 XHeader,
 Scroller
},
mounted() {
 this.$nextTick(() => {
 this.$refs.scrollerBottom.reset({top: 0})
 })
 this.loadMore()
},
data() {
 return {
 list: [],
 pullupDefaultConfig: pullupDefaultConfig,
 pulldownDefaultConfig: pulldownDefaultConfig
 }
},
methods: {
 fetchData(cb) {
 axios.get('http://localhost:3000/').then(response => {
  this.$nextTick(() => {
  this.$refs.scrollerBottom.reset()
  })
  cb(response.data)
 })
 },
 refresh() {
 this.fetchData(data => {
  this.list = data.list
  this.$refs.scrollerBottom.enablePullup()
  this.$refs.scrollerBottom.donePulldown()
 })
 },
 loadMore() {
 this.fetchData(data => {
  if (this.list.length >= 10) {
  this.$refs.scrollerBottom.disablePullup()
  }
  this.list = this.list.concat(data.list)
  this.$refs.scrollerBottom.donePullup()
 })
 }
}
}
</script>
<style lang="less">
.box {
padding: 5px 10px 5px 10px;
&:first-child {
 padding: 0 10px 5px 10px;
}
&:last-child {
 padding: 5px 10px 0 10px;
}
}
.list {
background-color: #fff;
border-radius: 4px;
border: 1px solid #f0f0f0;
padding: 30px;
}
.xs-plugin-pulldown-container {
line-height: 40px;
}
.xs-plugin-pullup-container {
line-height: 40px;
}
</style>

来源:https://tomoya92.github.io/2018/01/05/vuejs-vux-refresh-loadmore/

标签:vue.js,vux,下拉加载
0
投稿

猜你喜欢

  • TensorFlow通过文件名/文件夹名获取标签,并加入队列的实现

    2023-02-14 17:08:47
  • 利用Anaconda完美解决Python 2与python 3的共存问题

    2022-05-16 17:51:43
  • javascript定时变换图片实例代码

    2024-04-17 10:24:14
  • vue2.0+webpack环境的构造过程

    2024-05-08 10:42:00
  • python方法如何实现字符串反转

    2022-10-26 22:07:03
  • 浅析使用JDBC操作MySQL需要添加Class.forName("com.mysql.jdbc.Driver")

    2024-01-21 23:08:48
  • VSCode配置python环境及中文问题解决方法

    2022-07-14 15:39:02
  • python使用opencv实现马赛克效果示例

    2022-10-16 04:23:30
  • 关于python3 opencv 图像二值化的问题(cv2.adaptiveThreshold函数)

    2022-08-31 22:22:50
  • layer ui 导入文件之前传入数据的实例

    2023-08-15 11:39:17
  • Django debug为True时,css加载失败的解决方案

    2022-05-07 01:17:53
  • python读取excel进行遍历/xlrd模块操作

    2022-11-09 18:44:51
  • python遍历文件目录、批量处理同类文件

    2021-10-19 14:58:12
  • Python技法之如何用re模块实现简易tokenizer

    2021-08-04 17:03:52
  • 浅谈Pycharm调用同级目录下的py脚本bug

    2023-09-12 15:39:27
  • Python 并行化执行详细解析

    2021-09-23 22:20:52
  • vue3中使用ref和emit来减少props的使用示例详解

    2024-04-27 16:02:02
  • SQL Server DATEDIFF() 函数用法

    2024-01-17 16:18:16
  • Python实现贪吃蛇小游戏(单人模式)

    2023-09-26 23:14:42
  • vue项目中less的一些使用小技巧

    2023-07-02 16:51:33
  • asp之家 网络编程 m.aspxhome.com