在Vant的基礎(chǔ)上封裝下拉日期控件的代碼示例
需求分析
在實際項目中,表單里面的日期選擇是常用的組件。Vant有提供日期組件,但是居然沒有提供下拉形式的日期組件,不過該有的元件都有,就自己封裝一個。
封裝組件過程中我們要解決:
- 和表單的樣式能兼容
- 錯誤提示
- 參數(shù)問題
- 事件機制
- 格式化
解決問題
就給新的組件取名為 VantFieldDate。
期望使用的時候是這樣的
<vant-field-date label="發(fā)布時間" v-model="formData.publishDate" type="datetime" :max-date="new Date()" />
具體實現(xiàn),我貼上代碼詳細講解。
<template>
<div class="vant-field-date">
<van-cell
:title="label"
:class="{'readonly': readonly, 'placeholder' : text}"
:is-link="!readonly"
:required="required"
@click="show">
<!-- 顯示當前值,沒有值顯示提示文字 -->
{{ text ? text : placeholder }}
<!-- 自定義錯誤顯示 -->
<div
v-if="$attrs.error"
v-text="$attrs['error-message']"
class="van-field__error-message"
/>
</van-cell>
<!-- 用 actionsheet 來包裹彈出層日期控件 -->
<van-actionsheet v-model="isShowPicker">
<!-- $attrs 可以把根節(jié)點的attr放到目標組件上,如此可以像使用 DatePicker 組件一樣使用這個新組件 -->
<van-datetime-picker
v-bind="$attrs"
:type="type"
title="請選擇日期"
:min-date="minDate"
:max-date="maxDate"
@cancel="cancel"
@confirm="confirm"
/>
</van-actionsheet>
</div>
</template>
<script>
export default {
name: 'VantFieldDate',
inheritAttrs: false, // https://cn.vuejs.org/v2/api/#inheritAttrs
props: {
value: {
type: [Number, Date],
default: undefined // 值不能是 null,DatePicker會報錯
},
// Cell 顯示的文字
label: {
type: String,
default: null
},
// 必填的星號
required: {
type: Boolean,
default: false
},
// 只讀狀態(tài)
readonly: {
type: Boolean,
default: false
},
// 占位提示文字
placeholder: {
type: String,
default: '請選擇'
},
// 展示的格式化
format: {
type: String,
default: null
}
},
data() {
return {
selectedItem: null,
isShowPicker: false
}
},
computed: {
// 展示的格式化,時間提交的值是Date類型數(shù)據(jù)
formatFormula() {
if(this.format){
return this.format
} else if (this.type === 'date') {
return 'yyyy-MM-dd'
} else if (this.type === 'datetime') {
return 'yyyy-MM-dd hh:mm'
} else if (this.type === 'time') {
return 'hh:mm'
} else if (this.type === 'year-month') {
return 'yyyy-MM'
}
},
text() {
return this.value ? this.dateFormat(this.value, this.formatFormula) : ''
}
},
methods: {
dateFormat: (value, format) => {
if (!value) return
if (!(value instanceof Date)) {
value = new Date(value)
}
let o = {
'M+': value.getMonth() + 1, // month
'd+': value.getDate(), // day
'h+': value.getHours(), // hour
'm+': value.getMinutes(), // minute
's+': value.getSeconds(), // second
'q+': Math.floor((value.getMonth() + 3) / 3), // quarter
'S': value.getMilliseconds() // millisecond
}
if (!format || format === '') {
format = 'yyyy-MM-dd hh:mm:ss'
}
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (value.getFullYear() + '').substr(4 - RegExp.$1.length))
}
for (let k in o) {
if (new RegExp('(' + k + ')').test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
}
}
return format
},
show() {
if (!this.readonly) {
this.isShowPicker = true
}
},
confirm(value) {
// 更新 v-model 綁定的 value 值,第二個參數(shù)是毫秒數(shù),第三個參數(shù)是原始值,根據(jù)自己的項目的數(shù)據(jù)結(jié)構(gòu)來修改
// input 事件同時也會觸發(fā) vee-validate 的驗證事件
this.$emit('input', value.getTime(), value)
// onChange事件,雖然重寫 @input可以實現(xiàn),但這樣會破壞 v-model 寫法。
this.$emit('change', value.getTime(), value)
this.cancel()
},
// 隱藏彈框
cancel() {
this.isShowPicker = false
}
}
}
</script>
效果

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue中使用create-keyframe-animation與動畫鉤子完成復(fù)雜動畫
這篇文章主要介紹了Vue中使用create-keyframe-animation與動畫鉤子完成復(fù)雜動畫,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
基于vue cli 通過命令行傳參實現(xiàn)多環(huán)境配置
這篇文章主要介紹了vue項目通過命令行傳參實現(xiàn)多環(huán)境配置(基于@vue/cli)的相關(guān)資料,需要的朋友可以參考下2018-07-07
前端vue框架select下拉數(shù)據(jù)量過大造成卡頓問題解決辦法
這篇文章主要給大家介紹了關(guān)于前端vue框架select下拉數(shù)據(jù)量過大造成卡頓問題解決辦法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用select具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
解決Vue3.0刷新頁面警告[Vue Router warn]:No match 
這篇文章主要介紹了解決Vue3.0刷新頁面警告[Vue Router warn]:No match found for location with path /xxx問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03

