vue-antd?form組件封裝全過程
表單項–公用組件(vue-antd)
1、廢話不多說,先看具體效果
效果圖

表單校驗圖:

校驗均寫在組件之中,跟當(dāng)前頁面文件無任何關(guān)系,當(dāng)前頁面只接受組件傳遞過來的參數(shù)…
2、組件代碼如下
在vue項目中,此處采.js文件形式寫組件,并沒用.vue文件去寫…將表單項中的input和change事件暴露出來。。。

export default {
props: ['value'],
template: `
<div>
<p v-if='typeId == 1' style='width: 60%;display: flex;justify-content: space-between;align-items: center;'>
<span>預(yù)計到達蒸罐地時間:</span>
<a-date-picker
show-time
:value='SteamtankTimeVal'
:allowClear="false"
placeholder="選擇時間"
@change="handleSteamtankTimeInput"
/>
</p>
<p v-if='typeId == 1' style='width: 80%;display: flex;justify-content: flex-end;align-items: center;'>
<span>預(yù)計蒸罐所需時間:</span>
<a-col :span="8">
<a-input
addon-after="小時"
:value='tankTime'
:disabled='needInput'
@change='handleTankTime'
/>
</a-col>
</p>
<p style='width: 60%;display: flex;justify-content: space-between;align-items: center;'>
<span>預(yù)計發(fā)車時間:</span>
<a-date-picker
show-time
:value='DepartureTimeVal'
:allowClear="false"
placeholder="選擇時間"
@change="handleDepartureTimeInput"
/>
</p>
<p style='width: 80%;display: flex;justify-content: flex-end;align-items: center;'>
<span>預(yù)計到裝貨地所需時間:</span>
<a-col :span="8">
<a-input
addon-after="小時"
:disabled='needInput'
:value='needLoadAdressTime'
@change='handleNeedLoadAdressTime'
/>
</a-col>
</p>
<p style='width: 60%;display: flex;justify-content: space-between;align-items: center;'>
<span>預(yù)計到達裝貨地時間:</span>
<a-date-picker
show-time
:value='LoadAddressTimeVal'
:allowClear="false"
placeholder="選擇時間"
@change="handleLoadAddressTimeInput"
/>
</p>
<p style='width: 80%;display: flex;justify-content: flex-end;align-items: center;'>
<span>預(yù)計裝貨所需時間:</span>
<a-col :span="8">
<a-input
addon-after="小時"
:value='loadTime'
:disabled='needInput'
@change='handleLoadFinishTime'
/>
</a-col>
</p>
<p style='width: 60%;display: flex;justify-content: space-between;align-items: center;'>
<span>預(yù)計裝貨完成時間:</span>
<a-date-picker
show-time
:value='LoadFinishTimeVal'
:allowClear="false"
placeholder="選擇時間"
@change="handleLoadFinishTimeInput"
/>
</p>
<p style='width: 80%;display: flex;justify-content: flex-end;align-items: center;'>
<span>預(yù)計路途所需時間:</span>
<a-col :span="8">
<a-input
addon-after="小時"
:value='roadTime'
:disabled='needInput'
@change='handleroadFinishTime'
/>
</a-col>
</p>
<p style='width: 60%;display: flex;justify-content: space-between;align-items: center;'>
<span>預(yù)計到達卸貨地時間:</span>
<a-date-picker
show-time
:value='UnloadAddressTimeVal'
:allowClear="false"
placeholder="選擇時間"
@change="handleUnloadAddressTimeInput"
/>
</p>
<p style='width: 80%;display: flex;justify-content: flex-end;align-items: center;'>
<span>預(yù)計卸貨所需時間:</span>
<a-col :span="8">
<a-input
addon-after="小時"
:value='unLoadTime'
:disabled='needInput'
@change='handleUnLoadFinishTime'
/>
</a-col>
</p>
<p style='width: 60%;display: flex;justify-content: space-between;align-items: center;'>
<span>預(yù)計卸貨完成時間:</span>
<a-date-picker
show-time
:value='UnloadFinishTimeVal'
:allowClear="false"
placeholder="選擇時間"
@change="handleUnloadFinishTimeInput"
/>
</p>
</div>
`,
watch: {
value(val = {}) {
this.typeId = val.typeId || null
this.tankTime = val.tankTime || null
this.loadTime = val.loadTime || null
this.unLoadTime = val.unLoadTime || null
this.needLoadAdressTime = val.needLoadAdressTime || null
this.roadTime = val.roadTime || null
this.SteamtankTimeVal = val.steamtankTime || null
this.DepartureTimeVal = val.departureTime || null
this.LoadAddressTimeVal = val.loadAddressTime || null
this.LoadFinishTimeVal = val.loadFinishTime || null
this.UnloadAddressTimeVal = val.unLoadAddressTime || null
this.UnloadFinishTimeVal = val.unLoadFinishTime || null
}
},
data() {
let {
steamtankTime,
departureTime,
unLoadFinishTime,
unLoadAddressTime,
loadAddressTime,
loadFinishTime,
ydccId,
typeId,
tankTime,
loadTime,
unLoadTime,
needLoadAdressTime,
roadTime
} = this.value
return {
moment,
ydccId: ydccId || null,
typeId: typeId || null,
SteamtankTimeVal: steamtankTime || null,
DepartureTimeVal: departureTime || null,
LoadAddressTimeVal: loadAddressTime || null,
LoadFinishTimeVal: loadFinishTime || null,
UnloadAddressTimeVal: unLoadAddressTime || null,
UnloadFinishTimeVal: unLoadFinishTime || null,
tankTime: tankTime || null,
loadTime: loadTime || null,
unLoadTime: unLoadTime || null,
needLoadAdressTime: needLoadAdressTime || null,
roadTime: roadTime || null,
minutes30: 1800000,
hour2: 7200000,
needInput: true,
}
},
methods: {
async getLoadTime(steamTankTime) {
let minutes30 = this.minutes30
let hour2 = this.hour2
let params = {
ydccId: this.ydccId,
steamTankAddressId: ''
}
let result = await predictYdccTime(params)
if (result.code == 200) {
let streamAddressToSendAddressTime = Number(result.data.streamAddressToSendAddressTime) * 1.8 //蒸罐地到發(fā)貨地時間(ms)
let ydccSendToRecTime = Number(result.data.ydccSendToRecTime) * 1.8 //發(fā)貨地到收貨地時間(ms)
this.needLoadAdressTime = ((streamAddressToSendAddressTime) / 3600000).toFixed(2)
this.roadTime = ((ydccSendToRecTime - hour2) / 3600000).toFixed(2)
this.DepartureTimeVal = moment(Number(steamTankTime) + minutes30)
this.LoadAddressTimeVal = moment(Number(moment(this.DepartureTimeVal).format('x')) + streamAddressToSendAddressTime)
this.LoadFinishTimeVal = moment(Number(moment(this.LoadAddressTimeVal).format('x')) + hour2)
this.UnloadAddressTimeVal = moment(Number(moment(this.LoadFinishTimeVal).format('x')) + ydccSendToRecTime)
this.UnloadFinishTimeVal = moment(Number(moment(this.UnloadAddressTimeVal).format('x')) + hour2)
let dataTime = {
steamtankTime: moment(steamTankTime),
departureTime: this.DepartureTimeVal,
loadAddressTime: this.LoadAddressTimeVal,
loadFinishTime: this.LoadFinishTimeVal,
unLoadAddressTime: this.UnloadAddressTimeVal,
unLoadFinishTime: this.UnloadFinishTimeVal,
typeId: 1,
needLoadAdressTime: this.needLoadAdressTime,
roadTime: this.roadTime
}
this.triggerChange(dataTime)
this.needInput = false
} else {
//.....
}
},
async getDepartureTime(departureTime) {
let hour2 = this.hour2
let params = {
ydccId: this.ydccId,
steamTankAddressId: ''
}
let result = await predictYdccTime(params)
if (result.code == 200) {
let streamAddressToSendAddressTime = Number(result.data.streamAddressToSendAddressTime) * 1.8 //發(fā)車到發(fā)貨地時間(ms)
let ydccSendToRecTime = Number(result.data.ydccSendToRecTime) * 1.8 //發(fā)貨地到收貨地時間(ms)
this.needLoadAdressTime = (0 * 3600000).toFixed(2)
this.roadTime = ((ydccSendToRecTime - hour2) / 3600000).toFixed(2)
this.LoadAddressTimeVal = moment(Number(departureTime) + streamAddressToSendAddressTime)
this.LoadFinishTimeVal = moment(Number(moment(this.LoadAddressTimeVal).format('x')) + hour2)
this.UnloadAddressTimeVal = moment(Number(moment(this.LoadFinishTimeVal).format('x')) + ydccSendToRecTime)
this.UnloadFinishTimeVal = moment(Number(moment(this.UnloadAddressTimeVal).format('x')) + hour2)
let dataTime = {
steamtankTime: moment(departureTime),
departureTime: this.DepartureTimeVal,
loadAddressTime: this.LoadAddressTimeVal,
loadFinishTime: this.LoadFinishTimeVal,
unLoadAddressTime: this.UnloadAddressTimeVal,
unLoadFinishTime: this.UnloadFinishTimeVal,
typeId: 0,
needLoadAdressTime: this.needLoadAdressTime,
roadTime: this.roadTime
}
this.triggerChange(dataTime)
this.needInput = false
} else {
//.....
}
},
// 設(shè)置時間間隔
handleTankTime(e) { //蒸罐所需時間
const { value } = e.target
let tankTime = this.changeTime(value)
this.minutes30 = tankTime
this.tankTime = value
this.DepartureTimeVal = moment(Number(moment(this.SteamtankTimeVal).format('x')) + tankTime)
this.triggerChange({ tankTime: this.tankTime })
this.triggerChange({ departureTime: this.DepartureTimeVal })
},
handleLoadFinishTime(e) { //裝貨所需時間
const { value } = e.target
let loadFinishTime = this.changeTime(value)
this.hour2 = loadFinishTime
this.loadTime = Number(value)
this.LoadFinishTimeVal = moment(Number(moment(this.LoadAddressTimeVal).format('x')) + loadFinishTime)
this.triggerChange({ loadFinishTime: this.LoadFinishTimeVal })
this.triggerChange({ loadTime: this.loadTime })
},
handleUnLoadFinishTime(e) { //卸貨所需時間
const { value } = e.target
let unLoadFinishTime = this.changeTime(value)
this.hour2 = unLoadFinishTime
this.unLoadTime = Number(value)
this.UnloadFinishTimeVal = moment(Number(moment(this.UnloadAddressTimeVal).format('x')) + unLoadFinishTime)
this.triggerChange({ unLoadFinishTime: this.UnloadFinishTimeVal })
this.triggerChange({ unLoadTime: this.unLoadTime })
},
handleNeedLoadAdressTime(e) { //到達裝貨地所需時間
const { value } = e.target
let unLoadNeedTime = this.changeTime(value)
this.needLoadAdressTime = Number(value)
this.LoadAddressTimeVal = moment(Number(moment(this.DepartureTimeVal).format('x')) + unLoadNeedTime)
this.triggerChange({ loadAddressTime: this.LoadAddressTimeVal })
this.triggerChange({ needLoadAdressTime: this.needLoadAdressTime })
},
handleroadFinishTime(e) { //路途所需時間
const { value } = e.target
let roadNeedTime = this.changeTime(value)
this.roadTime = Number(value)
this.UnloadAddressTimeVal = moment(Number(moment(this.LoadFinishTimeVal).format('x')) + roadNeedTime)
this.triggerChange({ unLoadAddressTime: this.UnloadAddressTimeVal })
this.triggerChange({ roadTime: this.roadTime })
},
changeTime(val) {
let value = Number(val)
let oneHour = 3600000
return value * oneHour
},
handleSteamtankTimeInput(_moment, dateString) {
this.SteamtankTimeVal = _moment
let steamTankTime = Number(moment(_moment).format('x'))
this.getLoadTime(steamTankTime)
this.triggerChange({ steamtankTime: this.SteamtankTimeVal })
},
handleDepartureTimeInput(_moment, dateString) {
this.DepartureTimeVal = _moment
let departureTime = Number(moment(_moment).format('x'))
this.getDepartureTime(departureTime);
this.triggerChange({ departureTime: this.DepartureTimeVal })
},
handleLoadAddressTimeInput(_moment, dateString) {
this.LoadAddressTimeVal = _moment
this.triggerChange({ loadAddressTime: this.LoadAddressTimeVal })
},
handleLoadFinishTimeInput(_moment, dateString) {
this.LoadFinishTimeVal = _moment
this.triggerChange({ loadFinishTime: this.LoadFinishTimeVal })
},
handleUnloadAddressTimeInput(_moment, dateString) {
this.UnloadAddressTimeVal = _moment
this.triggerChange({ unLoadAddressTime: this.UnloadAddressTimeVal })
},
handleUnloadFinishTimeInput(_moment, dateString) {
this.UnloadFinishTimeVal = _moment
this.triggerChange({ unLoadFinishTime: this.UnloadFinishTimeVal })
},
triggerChange(changedValue) {
let output = Object.assign({
steamtankTime: this.SteamtankTimeVal,
departureTime: this.DepartureTimeVal,
loadAddressTime: this.LoadAddressTimeVal,
loadFinishTime: this.LoadFinishTimeVal,
unLoadAddressTime: this.UnloadAddressTimeVal,
unLoadFinishTime: this.UnloadFinishTimeVal,
tankTime: this.tankTime,
loadTime: this.loadTime,
unLoadTime: this.unLoadTime,
needLoadAdressTime: this.needLoadAdressTime,
roadTime: this.roadTime,
typeId: this.typeId
}, changedValue)
this.$emit('input', output)
this.$emit('change', output)
}
}
代碼比較繁瑣,還未進行精簡操作…湊合看吧
3、在相應(yīng)地方進行引入


剩下的就該怎么玩,就怎么玩咯
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue開發(fā)環(huán)境中修改端口號的實現(xiàn)方法
這篇文章主要介紹了Vue開發(fā)環(huán)境中修改端口號的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
vue+el-element中根據(jù)文件名動態(tài)創(chuàng)建dialog的方法實踐
本文主要介紹了vue+el-element中根據(jù)文件名動態(tài)創(chuàng)建dialog的方法實踐,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
基于vue-router 多級路由redirect 重定向的問題
今天小編就為大家分享一篇基于vue-router 多級路由redirect 重定向的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
jenkins自動構(gòu)建發(fā)布vue項目的方法步驟
這篇文章主要介紹了jenkins自動構(gòu)建發(fā)布vue項目的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
后臺使用freeMarker和前端使用vue的方法及遇到的問題
這篇文章主要介紹了后臺使用freeMarker和前端使用vue的方法及遇到的問題,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06
Vue中的@blur事件 當(dāng)元素失去焦點時所觸發(fā)的事件問題
這篇文章主要介紹了Vue中的@blur事件 當(dāng)元素失去焦點時所觸發(fā)的事件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08

