网络编程
位置:首页>> 网络编程>> JavaScript>> 解决vant框架做H5时踩过的坑(下拉刷新、上拉加载等)

解决vant框架做H5时踩过的坑(下拉刷新、上拉加载等)

作者:zlinger1  发布时间:2024-05-13 09:44:31 

标签:vant,H5,下拉,上拉

1. 页面在手机端不能上下滑动,在PC端浏览器正常滑动

说明:在设置了overflow:auto;属性的前提下,H5页面在PC端浏览器里展示可以上下滑动,在ios上可正常滑动,在安卓手机 上不能上下滑动;这现象并不是ios和安卓兼容性问题!

原因:设置了touch-action: none;这属性为局部或者全局属性,将这条属性注释即可正常滑动。

2.使用PullRefresh和List列表实现下拉刷新和上拉加载时出现的问题

问题1. 下拉刷新时在手机上,不论滑到任何位置,只要下拉就刷新

原因:滑动的区间设置错了,此时滑动的区间应是此组件的父盒子,我将overflow:scroll设置给了最外层盒子

问题2. 上拉加载时展示的列表始终只包含当前某一页,而不是当前页和加载出的那一页

原因:请求接口的参数不应该是current++,也就是不应该是当前页数+1,而是始终保持当前页数,请求的size=current*size

问题3. 下拉时,当数据很少的情况下,页面的最下面部分被遮住

原因:给van-list设置了height,且height: 80%,van-list必须设置高,否则无法滑动

解决办法:设置最小高:min-height: calc(100vh - 100px); overflow: hidden;

问题4.上拉加载时出现重复加载问题

van-list的属性finished触发时间错误,如果直接放在@load方法中,则会出现一直请求加载

解决办法:将finished=true放在请求接口返回数据的方法里,当当前页面数据大于等于返回的总条数,finished=true,显示加载完成,不再触发load加载事件。

注:附上下拉刷新、上拉加载部分的代码


<template>
<van-pull-refresh v-model="isLoading" :head-height="20" @refresh="onRefresh">
 <van-list
 v-model="loading"
 :finished="finished"
 :offset="1"
 :immediate-check="false"
 :error.sync="error"
 finished-text="已全部加载完成"
 error-text="请求失败,点击重新加载"
 @load="onLoadList"
 >
 </van-list>
</van-pull-refresh>
</template>

<script>
data(){
return {
isLoading: false,
finished: false,
loading: false,
}
},
methods:{
getVideoList() {
 getVideoList(this.current, this.selectDisposeStatus,  this.searchValue).then(resp => {
 this.videoList = resp.data.records
 this.isVideoList = false
 if (this.videoList.length >= resp.data.total) {
  this.finished = true
 }
 }).catch(() => {
 this.error = true
 })
},
onRefresh() {
 this.current = 1
 this.getVideoList()
 this.isLoading = false
 this.$toast('刷新成功')
},
onLoadList() {
 this.current++
 this.loading = false
 this.getVideoList()
 this.$toast('加载成功')
},
}
</script>

补充知识:Vant与Element-ui出现Property '$notify' must be of type 'ElNotification'错误

遇到问题:

ERROR in /Users/oyo/projects/dataplatform/coconut/node_modules/vant/types/notify.d.ts 33:5 Subsequent property declarations must have the same type. Property ‘$notify' must be of type ‘ElNotification', but here has type ‘Notify'.

原因是两个组件库都在应用上添加了 $notify 方法;

解决方法是:只安装一个组件库, 另一个组件库按需引入

报错示例:

npm install vant element-ui

vant 和 element-ui 都有 $notify 方法, 会报错


import Vue from 'vue';
import Vant from 'vant';
import 'vant/lib/index.css';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(Vant);
Vue.use(ElementUI);

解决方案:


import Vue from 'vue';
import Vant from 'vant';
import 'vant/lib/index.css';

// 按需引入你用到的组件, 而不是安装整个组件库
import ElButton from 'element-ui/lib/button';
import 'element-ui/lib/theme-chalk/button.css';

Vue.use(Vant);
Vue.component('el-button', ElButton);

tsconfig.json
{
"compilerOptions": {
"paths": {
 // 指向正确的声明映射
 "element-ui/lib/button": ["node_modules/element-ui/types/button"]
}
}
}

来源:https://blog.csdn.net/weixin_41175327/article/details/103870210

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com