vant 组件中事件选择器的一些小用法

2024-08-23 10:52

本文主要是介绍vant 组件中事件选择器的一些小用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

获取当前所在的月份的月初和当天的日期

computed: {// 默然展示月初的第一天formattedFirstDayOfMonth() {const now = new Date();const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);const year = firstDayOfMonth.getFullYear();const month = (firstDayOfMonth.getMonth() + 1).toString().padStart(2, "0"); // 月份是从0开始的,所以加1const day = firstDayOfMonth.getDate().toString().padStart(2, "0");return `${year}年${month}月${day}日`;},//默认展示今天formattedToday() {const today = new Date();const year = today.getFullYear();const month = (today.getMonth() + 1).toString().padStart(2, "0"); // 月份是从0开始的,所以加1const day = today.getDate().toString().padStart(2, "0");return `${year}年${month}月${day}日`;},},

我们在写页面的时候 经常会遇到事件的选择器  象我做H5 的时候,我经常用的组件库 就是vant 组件库

 举个简单的小例子

 就是 下面的日期变化时 上面框里面的日期也要跟着改变 然后点击的时候 选择一个区间的范围

<template>
<van-popupv-model="dateShow"position="bottom"round:style="{ height: '52%' }"><div class="day_box"><div class="customize_text">自定义</div><imgsrc="../../assets/information/close.png"alt=""class="close-icon"@click="onClose"/></div><div class="date_Range"><divclass="date_Range_left"@click="onClickday(0)":class="{ date_Range_right: currentday == 0 }"><div v-if="firstFormatValue == null">{{ formattedFirstDayOfMonth }}</div><div v-else>{{ firstFormatValue }}</div></div><div class="date_Range_line"></div><divclass="date_Range_left"@click="onClickday(1)":class="{ date_Range_right: currentday == 1 }"><div v-if="currentFormatValue == null">{{ formattedToday }}</div><div v-else>{{ currentFormatValue }}</div></div></div><div class="date-currentDate-box"><van-datetime-pickerref="datetimePicker"v-model="currentDate"type="date"title="选择年月日":min-date="minDate":max-date="maxDate":visible-item-count="3"@change="handleChange"/><div class="btn" @click.stop="onConfirm">确认</div></div></van-popup></template>
<script>
export default {data() {return {dateShow: false,minDate: new Date(2020, 0, 1),maxDate: new Date(),currentDate: new Date(),formattedDate: "", //日期currentday: 1,firstFormatValue: null,currentFormatValue: null,addFormatValue: "",};},computed: {// 默然展示月初的第一天formattedFirstDayOfMonth() {const now = new Date();const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);const year = firstDayOfMonth.getFullYear();const month = (firstDayOfMonth.getMonth() + 1).toString().padStart(2, "0"); // 月份是从0开始的,所以加1const day = firstDayOfMonth.getDate().toString().padStart(2, "0");return `${year}年${month}月${day}日`;},//默认展示今天formattedToday() {const today = new Date();const year = today.getFullYear();const month = (today.getMonth() + 1).toString().padStart(2, "0"); // 月份是从0开始的,所以加1const day = today.getDate().toString().padStart(2, "0");return `${year}年${month}月${day}日`;},},methods: {handleChange(pickerInstance) {if (this.currentday == 0) {this.firstFormatValue =pickerInstance.getValues()[0] +"年" +pickerInstance.getValues()[1] +"月" +pickerInstance.getValues()[2] +"日";} else if (this.currentday == 1) {this.currentFormatValue =pickerInstance.getValues()[0] +"年" +pickerInstance.getValues()[1] +"月" +pickerInstance.getValues()[2] +"日";}},onConfirm() {// const selectedDate = this.$refs.datetimePicker.value;// this.formattedDate = `${this.firstFormatValue}-${this.currentFormatValue}`;// this.addFormatValue = this.formatDate(selectedDate);// console.log(this.addFormatValue);// this.dateShow = false;const selectedDate = this.$refs.datetimePicker.value;// 新增判断if (this.firstFormatValue === null || this.currentFormatValue === null) {this.addFormatValue = `${this.formattedFirstDayOfMonth}-${this.formattedToday}`;console.log(this.addFormatValue);} else {this.formattedDate = `${this.firstFormatValue}-${this.currentFormatValue}`;}this.dateShow = false;},formatDate(date) {const year = date.getFullYear();const month = (date.getMonth() + 1).toString().padStart(2, "0"); // 月份是从0开始的,所以加1const day = date.getDate().toString().padStart(2, "0");return `${year}年${month}月${day}日`;},onCustomShow() {this.dateShow = true;},},
};
</script>

 

这篇关于vant 组件中事件选择器的一些小用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1099176

相关文章

JDK21对虚拟线程的几种用法实践指南

《JDK21对虚拟线程的几种用法实践指南》虚拟线程是Java中的一种轻量级线程,由JVM管理,特别适合于I/O密集型任务,:本文主要介绍JDK21对虚拟线程的几种用法,文中通过代码介绍的非常详细,... 目录一、参考官方文档二、什么是虚拟线程三、几种用法1、Thread.ofVirtual().start(

Vue3视频播放组件 vue3-video-play使用方式

《Vue3视频播放组件vue3-video-play使用方式》vue3-video-play是Vue3的视频播放组件,基于原生video标签开发,支持MP4和HLS流,提供全局/局部引入方式,可监听... 目录一、安装二、全局引入三、局部引入四、基本使用五、事件监听六、播放 HLS 流七、更多功能总结在 v

Java8 Collectors.toMap() 的两种用法

《Java8Collectors.toMap()的两种用法》Collectors.toMap():JDK8中提供,用于将Stream流转换为Map,本文给大家介绍Java8Collector... 目录一、简单介绍用法1:根据某一属性,对对象的实例或属性做映射用法2:根据某一属性,对对象集合进行去重二、Du

Python中isinstance()函数原理解释及详细用法示例

《Python中isinstance()函数原理解释及详细用法示例》isinstance()是Python内置的一个非常有用的函数,用于检查一个对象是否属于指定的类型或类型元组中的某一个类型,它是Py... 目录python中isinstance()函数原理解释及详细用法指南一、isinstance()函数

Python中的sort方法、sorted函数与lambda表达式及用法详解

《Python中的sort方法、sorted函数与lambda表达式及用法详解》文章对比了Python中list.sort()与sorted()函数的区别,指出sort()原地排序返回None,sor... 目录1. sort()方法1.1 sort()方法1.2 基本语法和参数A. reverse参数B.

vue监听属性watch的用法及使用场景详解

《vue监听属性watch的用法及使用场景详解》watch是vue中常用的监听器,它主要用于侦听数据的变化,在数据发生变化的时候执行一些操作,:本文主要介绍vue监听属性watch的用法及使用场景... 目录1. 监听属性 watch2. 常规用法3. 监听对象和route变化4. 使用场景附Watch 的

Java Instrumentation从概念到基本用法详解

《JavaInstrumentation从概念到基本用法详解》JavaInstrumentation是java.lang.instrument包提供的API,允许开发者在类被JVM加载时对其进行修改... 目录一、什么是 Java Instrumentation主要用途二、核心概念1. Java Agent

Java 中 Optional 的用法及最佳实践

《Java中Optional的用法及最佳实践》在Java开发中,空指针异常(NullPointerException)是开发者最常遇到的问题之一,本篇文章将详细讲解Optional的用法、常用方... 目录前言1. 什么是 Optional?主要特性:2. Optional 的基本用法2.1 创建 Opti

Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧

《Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧》本文将通过实际代码示例,深入讲解Python函数的基本用法、返回值特性、全局变量修改以及异常处理技巧,感兴趣的朋友跟随小编一起看看... 目录一、python函数定义与调用1.1 基本函数定义1.2 函数调用二、函数返回值详解2.1 有返

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu