el-date-picker 通常都是時間選擇器獲取焦點的時候獲取當前時間,現(xiàn)在的需求是表單進入時間框默認當前年月日分秒,并且可以再次獲取選中時間。
下面是我的解決辦法,希望可以幫到你們!
1.首先要v-model綁定時間選擇器值
<el-date-picker
v-model="logForm.date"
type="datetime"
style="width:200px"
placeholder="選擇日期時間"
value-format="yyyy-MM-dd HH:mm:ss"
format="yyyy-MM-dd HH:mm:ss"
defaule-value="dafaultValue"
>
</el-date-picker>
2.在Date方法中首先要使用new Date獲取當前時間 其次是時分秒 最后進行拼接需要的格式 (比如yyyy-MM-dd 或者是yyyy-MM-dd HH:mm:ss 我這邊是拼接的獲取的是當前年月日時分秒)?
3.最后一步使用?this.$set(target, key, value)
target:要更改的數(shù)據(jù)源(可以是數(shù)據(jù)對象或者數(shù)組)
key:要更改的具體數(shù)據(jù)
value :重新賦的值
this.$set定義:(實際上向響應式對象中添加一個屬性,并確保這個新屬性同樣是響應式的,且觸發(fā)視圖更新。它必須用于向響應式對象上添加新屬性)文章來源:http://www.zghlxwxcb.cn/news/detail-513706.html
Date(){
const nowDate = new Date();
const date = {
year: nowDate.getFullYear(),
month: nowDate.getMonth() + 1,
date: nowDate.getDate(),
hours: nowDate.getHours(),
minutes: nowDate.getMinutes(),
seconds: nowDate.getSeconds()
};
const newmonth = date.month > 10 ? date.month : "0" + date.month;
const newday = date.date > 10 ? date.date : "0" + date.date;
const newminutes = date.minutes < 10 ? "0" + date.minutes : date.minutes;
const newseconds = date.seconds < 10 ? "0" + date.seconds : date.seconds;
const value =
date.year +
"-" +
newmonth +
"-" +
newday +
" " +
date.hours +
":" +
newminutes +
":" +
newseconds;
this.$set(this.logForm, "date", value);
}
運用以上代碼 就可以實現(xiàn)獲取到當前時間并進行再次改變時間數(shù)據(jù)文章來源地址http://www.zghlxwxcb.cn/news/detail-513706.html
到了這里,關于Vue+Element ui el-date-picker默認當前年月日時分秒并且可再次選擇的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!