微信小程序 picker 组件使用&&用mpvue开发时使用

一、微信小程序 picker 组件

<view class="section">
  <view class="section__title">日期选择器</view>
  <picker mode="date" value="{{date}}" start="2015-09-01" end="2017-09-01" bindchange="bindDateChange">
    <view class="picker">
      当前选择: {{date}}
    </view>
  </picker>
</view>
  bindDateChange: function(e) {
    console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },

二、在mpvue中使用

<picker mode="date" :value="date" fields='day' @change="pickerChange"  class="change_date">
  <view class="picker">
    当前选择: {{date}}
  </view>
</picker>

(1)初始化date:‘2020-04-19’

  pickerChange: function (e) {
//与原生小程序本身不同,这里e.mp.detail.value拿到的是你选择的值
  this.date = e.mp.detail.value
},

(2)初始化日期为今天

<template>
	<div>
		<picker mode="date" :value="date" fields='day' @change="pickerChange" class="change_date" :start="pickerStart" end="2100-02-02">
			<view class="picker">
				当前选择: {{date}}
			</view>
		</picker>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				date: '2020-04-19',
				pickerStart: '2020-04-19'
			};
		},
		mounted() {
			// 设置时间
			let today = this.getToday();
			this.pickerStart = today
		},
		methods: {
			pickerChange: function(e) {
				//与小程序本身不同,这里e.mp.detail.value拿到的是你选择的值
				this.date = e.mp.detail.value
			},
			getToday() {
				let myDate = new Date();
				let myMonth = myDate.getMonth() + 1;
				if (myMonth < 10) {
					myMonth = '0' + myMonth; //补0
				}
				let mydate = myDate.getDate();
				if (myDate.getDate() < 10) {
					mydate = '0' + myDate.getDate(); //补0
				}
				let today = myDate.getFullYear() + '-' + myMonth + '-' + mydate;
				return today
		},
	},
};
</script>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值