這篇文章將為大家詳細講解有關使用Vant怎么封裝一個下拉日期控件,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
淅川網站制作公司哪家好,找創新互聯!從網頁設計、網站建設、微信開發、APP開發、響應式網站設計等網站項目制作,到程序開發,運營維護。創新互聯2013年開創至今到現在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選創新互聯。
需求分析
在實際項目中,表單里面的日期選擇是常用的組件。Vant有提供日期組件,但是居然沒有提供下拉形式的日期組件,不過該有的元件都有,就自己封裝一個。
封裝組件過程中我們要解決:
和表單的樣式能兼容
錯誤提示
參數問題
事件機制
格式化
解決問題
就給新的組件取名為 VantFieldDate
。
期望使用的時候是這樣的
<vant-field-date label="發布時間" v-model="formData.publishDate" type="datetime" :max-date="new Date()" />
具體實現,我貼上代碼詳細講解。
<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 可以把根節點的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 }, // 只讀狀態 readonly: { type: Boolean, default: false }, // 占位提示文字 placeholder: { type: String, default: '請選擇' }, // 展示的格式化 format: { type: String, default: null } }, data() { return { selectedItem: null, isShowPicker: false } }, computed: { // 展示的格式化,時間提交的值是Date類型數據 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 值,第二個參數是毫秒數,第三個參數是原始值,根據自己的項目的數據結構來修改 // input 事件同時也會觸發 vee-validate 的驗證事件 this.$emit('input', value.getTime(), value) // onChange事件,雖然重寫 @input可以實現,但這樣會破壞 v-model 寫法。 this.$emit('change', value.getTime(), value) this.cancel() }, // 隱藏彈框 cancel() { this.isShowPicker = false } } } </script>
關于使用Vant怎么封裝一個下拉日期控件就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
分享題目:使用Vant怎么封裝一個下拉日期控件
URL網址:http://m.2m8n56k.cn/article48/jdcdep.html
成都網站建設公司_創新互聯,為您提供App設計、定制網站、手機網站建設、網頁設計公司、微信公眾號、搜索引擎優化
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯