vant 中van-list的用法说明


Posted in Javascript onNovember 11, 2020

van-list里面的元素不能有float样式,否则会连续触发 load 事件

原代码

<template>
 <div class="about">
  <van-tabs v-model="active" sticky @change="getTypeDate">
   <van-tab v-for="(tab) in typeList" :title="tab.name" :key="tab.id">
    <div :style="{height: contentHeight}" class="pic-content">
     <van-list
      :finished="finished"
      :finished-text="finishedText"
      v-model="loading"
      :offset="10"
      :immediate-check="false"
      @load="getserviceList"
     >
     <!------------------------------------------------- 修改前代码 --------------------------------------------->
       /*<div
        class="pic-box"
        v-for="(serve) in serviceList"
        :key="serve.id"
        @click="router(serve)"
       >
        <div class="pic-item">
         <img
          v-if="serve.picturePath"
          :src="$BASE_PICTUREPATH_URL + serve.picturePath.split(',')[0]"
         >
        </div>
        <p>{{serve.name}}</p>
        <p class="price-red">¥{{serve.price}}</p>
       </div>*/
       <!------------------------------------------------- 修改前代码 --------------------------------------------->
     </van-list>
    </div>
   </van-tab>
  </van-tabs>
 </div>
</template>
<script>
import { Tab, Tabs, List, Cell, Row, Col } from "vant";
import { FetchServeType, FetchServeList } from "../apis/serve.js";

export default {
 data() {
  return {
   active: 0,
   typeList: [],
   serviceList: [],
   type: "",
   finishedText: "",
   finished: false,
   pageNum: 1,
   pageSize: 10,
   contentHeight: 0,
   loading: false
  };
 },
 mounted() {
  this.getOrderStyle();
  this.contentHeight = document.documentElement.clientHeight - 66 - 40 + "px";
 },
 methods: {
  async getOrderStyle() {
   let res = await FetchServeType();
   if (res.data && res.data.success) {
    this.typeList = res.data.data;
    this.type = res.data.data[0].name;
    this.getTypeDate();
   }
  },
  getTypeDate() {
   this.pageNum = 1;
   this.type = this.typeList[this.active].name;
   this.serviceList = [];
   this.finishedText = "";
   this.finished = false;
   this.getserviceList();
  },
  async getserviceList() {
   let toast = this.$toast.loading({
    mask: true,
    message: "加载中..."
   });
   const { type, pageNum, pageSize } = this;
   let params = {
    type,
    pageNum,
    pageSize
   };
   let res = await FetchServeList(params);
   this.loading = false;
   toast.close();
   if (res.data && res.data.success) {
    let list = (res.data.data && res.data.data.list) || [];
    if (pageNum > 1) {
     this.serviceList = [...this.serviceList, ...list];
    } else {
     this.serviceList = list;
    }
    // 如果当前页数 = 总页数,则已经没有数据
    if (res.data.data.pageNum === res.data.data.pages) {
     this.finished = true;
     this.finishedText = "- 没有更多了-";
    }
    // 如果总页数大于当前页码,页码+1
    if (res.data.data.pages > pageNum) {
     this.pageNum++;
    }
   }
   console.log("FetchServeList: ", this.serviceList);
  }
 }
};
</script>
<style lang="scss" scoped>
.pic-content {
 overflow-y: scroll;
 -webkit-overflow-scrolling: touch;
 .pic-box {
 /****************************修改前代码***************************/
  background-color: #fff;
  overflow: hidden;
  break-inside: avoid;
  box-sizing: border-box;
  margin-bottom: 0.7rem;
  padding: 0.8rem;
  width: 48%;
  height: 16rem;
  ~~float: left;~~ /**************不能有float样式*************/
  margin: 1%;
  border-radius: 4px;
   /****************************修改前代码***************************/
  p:nth-of-type(1) {
   padding: 0.8rem 0;
  }
  p:nth-of-type(2) {
   color: red;
  }
  .pic-item {
   height: 11rem;

   flex-direction: column;
   justify-content: center;
   overflow: hidden;
   img {
    width: 100%;
    height: auto;
    border-radius: 4px;
   }
  }
 }
}
</style>

// 修改后代码(注释部分为修改后代码)

<template>
 <div class="about">
  <van-tabs v-model="active" sticky @change="getTypeDate">
   <van-tab v-for="(tab) in typeList" :title="tab.name" :key="tab.id">
    <div :style="{height: contentHeight}" class="pic-content">
     <van-list
      :finished="finished"
      :finished-text="finishedText"
      v-model="loading"
      :offset="10"
      :immediate-check="false"
      @load="getserviceList"
     >
     <!------------------- 修改后代码 -------------------->
      /*<van-row>
       <van-col
        span="12"
        class="pic-box"
        v-for="(serve) in serviceList"
        :key="serve.id"
        @click="router(serve)"
       >
        <div class="pic-item">
         <img
          v-if="serve.picturePath"
          :src="$BASE_PICTUREPATH_URL + serve.picturePath.split(',')[0]"
         >
        </div>
        <p>{{serve.name}}</p>
        <p class="price-red">¥{{serve.price}}</p>
       </van-col>
      </van-row>*/
      <!------------------- 修改后代码 -------------------->
     </van-list>
    </div>
   </van-tab>
  </van-tabs>
 </div>
</template>
<script>
import { Tab, Tabs, List, Cell, Row, Col } from "vant";
import { FetchServeType, FetchServeList } from "../apis/serve.js";

export default {
 data() {
  return {
   active: 0,
   typeList: [],
   serviceList: [],
   type: "",
   finishedText: "",
   finished: false,
   pageNum: 1,
   pageSize: 10,
   contentHeight: 0,
   loading: false
  };
 },
 mounted() {
  this.getOrderStyle();
  this.contentHeight = document.documentElement.clientHeight - 66 - 40 + "px";
 },
 methods: {
  async getOrderStyle() {
   let res = await FetchServeType();
   if (res.data && res.data.success) {
    this.typeList = res.data.data;
    this.type = res.data.data[0].name;
    this.getTypeDate();
   }
  },
  getTypeDate() {
   this.pageNum = 1;
   this.type = this.typeList[this.active].name;
   this.serviceList = [];
   this.finishedText = "";
   this.finished = false;
   this.getserviceList();
  },
  async getserviceList() {
   let toast = this.$toast.loading({
    mask: true,
    message: "加载中..."
   });
   const { type, pageNum, pageSize } = this;
   let params = {
    type,
    pageNum,
    pageSize
   };
   let res = await FetchServeList(params);
   this.loading = false;
   toast.close();
   if (res.data && res.data.success) {
    let list = (res.data.data && res.data.data.list) || [];
    if (pageNum > 1) {
     this.serviceList = [...this.serviceList, ...list];
    } else {
     this.serviceList = list;
    }
    // 如果当前页数 = 总页数,则已经没有数据
    if (res.data.data.pageNum === res.data.data.pages) {
     this.finished = true;
     this.finishedText = "- 没有更多了-";
    }
    // 如果总页数大于当前页码,页码+1
    if (res.data.data.pages > pageNum) {
     this.pageNum++;
    }
   }
   console.log("FetchServeList: ", this.serviceList);
  }
 }
};
</script>
<style lang="scss" scoped>
.pic-content {
 overflow-y: scroll;
 -webkit-overflow-scrolling: touch;
 .pic-box {
 /************************ 修改后代码**************************/
  background-color: #fff;
  overflow: hidden;
  box-sizing: border-box;
  margin-bottom: 0.7rem;
  padding: 0.8rem;
  height: 16rem;
  border-radius: 4px;
  /************************ 修改后代码************************ **/
  p:nth-of-type(1) {
   padding: 0.8rem 0;
  }
  p:nth-of-type(2) {
   color: red;
  }
  .pic-item {
   height: 11rem;

   flex-direction: column;
   justify-content: center;
   overflow: hidden;
   img {
    width: 100%;
    height: auto;
    border-radius: 4px;
   }
  }
 }
}
</style>

补充知识:vant里 List 组件可以与 PullRefresh 组件结合使用的一个小提示与小坑坑

小提示

List 组件可以与 PullRefresh 组件结合使用,可以实现列表下拉刷新的效果,但是当下拉刷新后更新的数据展示在页面上不能撑满 List 列表中的内容的时候,他并不会主动触发列表刷新,以至于来填满列表。

可以给list组件添加ref属性,然后在下拉刷新后,在下拉刷新的事件里手动调用this.$refs.listRef(你的list的ref名称).check()来触发列表加载后续的数据

// list组件
<van-list
  v-model="loading"
  ref="listRef" // 1. 绑定ref
  :finished="finished"
  finished-text="没有更多了"
  :error.sync="error"
  error-text="请求失败,点击重新加载"
  @load="onLoad"
>
// 下拉刷新的事件
onRefresh() {
 ...刷新成功后
 // 2.手动去让下拉刷新后,去执行list列表的load事件
 this.$refs.listRef.check()
}

小坑坑

如果你把List 组件可以与 PullRefresh 组件结合使用封装成一个组件,然后在父组件中使用的时候,需要给封装的这个组件传list组件的v-model的值来控制list是否处于加载状态。

然后在父组件传 v-moel=“loading” 或者 :is-loading.sync=“loading” 传给子组件让他来控制子组件的list的v-model的控制load加载状态,按理说v-model 默认是 value 属性和 input 事件的组合,但是list组件的文件默认修改了,把传过去的value用 model: { prop: ‘loading' }修改了,所以我们在子组件接收的时候不能用value 要用loading

此图为vant的源码

vant 中van-list的用法说明

// 父组件 给子组件传list的v-model的值
:is-loading.sync="loading"
// 或写成
v-model="loading"

// 子组件 list组件
// 子组件不能用value接收 
// :value="isLoading"
// 应该写成loading
:loading="isLoading"

以上这篇vant 中van-list的用法说明就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
js 浮动层菜单收藏
Jan 16 Javascript
JS 巧妙获取剪贴板数据 Excel数据的粘贴
Jul 09 Javascript
javascript基础知识大集锦(二) 推荐收藏
Jan 13 Javascript
解决jquery插件冲突的问题
Jan 23 Javascript
JS设置CSS样式的方式汇总
Jan 21 Javascript
Bootstrap面板学习使用
Feb 09 Javascript
JavaScript轮播图简单制作方法
Feb 20 Javascript
AngularJS实现的select二级联动下拉菜单功能示例
Oct 25 Javascript
JQuery实现ajax请求的示例和注意事项
Dec 10 jQuery
laydate只显示时分 不显示秒的功能实现方法
Sep 28 Javascript
微信小程序使用自定义组件导航实现当前页面高亮
Jan 02 Javascript
react ant Design手动设置表单的值操作
Oct 31 Javascript
让Vue响应Map或Set的变化操作
Nov 11 #Javascript
vue项目中使用rem,在入口文件添加内容操作
Nov 11 #Javascript
VUE前端从后台请求过来的数据进行转换数据结构操作
Nov 11 #Javascript
Vue 防止短时间内连续点击后多次触发请求的操作
Nov 11 #Javascript
Vue 401配合Vuex防止多次弹框的案例
Nov 11 #Javascript
VUE-ElementUI 自定义Loading图操作
Nov 11 #Javascript
在elementui中Notification组件添加点击事件实例
Nov 11 #Javascript
You might like
php 删除记录同时删除图片文件的实现代码
2010/05/12 PHP
PHP反射机制用法实例
2014/08/28 PHP
php导出中文内容excel文件类实例
2015/07/06 PHP
php数组生成html下拉列表的方法
2015/07/20 PHP
PHP序列化/对象注入漏洞分析
2016/04/18 PHP
thinkPHP多表查询及分页功能实现方法示例
2017/07/03 PHP
PHP实现求连续子数组最大和问题2种解决方法
2017/12/26 PHP
实例讲解php实现多线程
2019/01/27 PHP
基于PHP实现解密或加密Cloudflar邮箱保护
2020/06/24 PHP
sencha touch 模仿tabpanel导航栏TabBar的实例代码
2013/10/24 Javascript
当达到输入长度时表单自动切换焦点
2014/04/06 Javascript
table insertRow、deleteRow定义和用法总结
2014/05/14 Javascript
jQuery的:parent选择器定义和用法
2014/07/01 Javascript
禁用页面部分JavaScript不是全部而是部分
2014/09/03 Javascript
JavaScript访问字符串中单个字符的两种方法
2015/07/03 Javascript
JavaScript 字符串常用操作小结(非常实用)
2016/11/30 Javascript
利用Console来Debug的10个高级技巧汇总
2018/03/26 Javascript
JavaScript实现数字前补“0”的五种方法示例
2019/01/03 Javascript
python3.3实现乘法表示例
2014/02/07 Python
python 获取当天每个准点时间戳的实例
2018/05/22 Python
python事件驱动event实现详解
2018/11/21 Python
基于python实现语音录入识别代码实例
2020/01/17 Python
Pycharm Plugins加载失败问题解决方案
2020/11/28 Python
python工具快速为音视频自动生成字幕(使用说明)
2021/01/27 Python
浅谈关于html5中图片抛物线运动的一些心得
2018/01/09 HTML / CSS
Lookfantastic俄罗斯:欧洲在线化妆品零售商
2019/08/06 全球购物
Janie and Jack美国官网:GAP旗下的高档童装品牌
2019/09/09 全球购物
屈臣氏乌克兰:Watsons UA
2019/10/29 全球购物
德国W家官网,可直邮中国的母婴商城:Windeln.de
2021/03/03 全球购物
《英英学古诗》教学反思
2014/04/11 职场文书
安康杯竞赛活动总结
2014/05/05 职场文书
求职简历自荐信
2014/06/18 职场文书
党委班子剖析材料
2014/08/21 职场文书
导游词之神仙居景区
2019/11/15 职场文书
MongoDB orm框架的注意事项及简单使用
2021/06/20 MongoDB
基于Python实现射击小游戏的制作
2022/04/06 Python