vue使用laydate時間插件的方法
之前在做vue項目時使用iviewUI庫中的DatePicker組件,發(fā)現(xiàn)DatePicker使用起來比較麻煩,尤其是對時間精確度上的限制不盡人意,操作起來也比較繁瑣,總之在處理一系列時間組件相互聯(lián)動上存在一大堆問題,比如

DatePicker時間組件
時間精確到分,組件1的value等于組件2的最小值,組件2的vlaue等于組件3的最小值,。。。依次類推,如果是時間精確到日,這個組件沒有任何問題,如果是精確到時分秒,這個組件的對時分秒的控制就不是那個靈敏了,在點擊時分秒之后才能識別出對時分秒的限制,而這種用戶體驗不夠友好。
所以,想到了以前用過的laydate時間插件
在vue中使用laydate
在vue組件中npm install laydate,然后在vue組件中直接引入:
import laydate from 'laydate'
然后在mounted中調(diào)用:
laydate.now();
發(fā)現(xiàn)chrome瀏覽器控制臺中報錯,laydate is not defined,然后把laydate.js放在靜態(tài)資源里面引入:
import laydate from '../../static/js/laydate.js'
發(fā)現(xiàn)chrome瀏覽器控制臺中依然是報錯,laydate is not defined,然后在html入口文件中引入:
<script src="./src/static/js/laydate.js"></script>
發(fā)現(xiàn)chrome瀏覽器控制臺中不報,laydate is not defined,但是又報另外一個錯誤:require is not defined,查看原碼發(fā)現(xiàn)是因為laydate.js中在引用css樣式表時未定義:require('./need/laydate.css');require('./skins/default/laydate.css');
最后使用laydate打包構(gòu)建后的文件:dist/laydate/laydate.min.js,把laydate.min.js和css粘貼到src/static/路徑下,注意js和css結(jié)構(gòu)不要改變
<script src="./src/static/laydate/laydate.min.js"></script>
然后在chrome瀏覽器中成功打印出了laydate.now()的值:2018-10-21
然后使用import方式引入laydate.min.js:
import laydate from '../../static/laydate/laydate.min.js'
chrome控制臺里有報錯:laydate.now is not a function
原因時laydate.min.js是直接把laydate對象注冊到了window上,本身并沒有export default laydate出口,所以不能使用這種方式引用,應(yīng)該使用:
import '../../static/laydate.min.js'
這樣引用后,chrome中成功打印laydate.now(); 2018-10-21
或者在入口文件index.html中使用cdn加速方式引入laydate.min.js也是可以的:
<script src="https://cdn.jsdelivr.net/npm/laydate@1.0.7/dist/laydate.min.js"></script>
當(dāng)然,如果項目中只是個別地方是使用到時間插件,建議采用import方式引入
laydate.min.js時間插件在vue組件中的使用方式
<template>
<div id="laydateIndex">
<div class="input-item">
<label for="">請選擇時間:</label>
<input type='text'
name='houseChangeTime'
placeholder='請選擇日期'
class='form-control'
onclick="laydate({ istime: true, format: 'YYYY/MM/DD hh:mm:ss' })" />
</div>
<div class="input-item">
<label for="">開始時間:</label>
<input type="text" name="" id="begintime" placeholder='請選擇日期'>
</div>
<div class="input-item">
<label for="">結(jié)束時間:</label>
<input type="text" name="" id="endtime" placeholder='請選擇日期'>
</div>
</div>
</template>
<script>
import '../../assets/js/laydate.min.js'
export default {
name: 'laydateIndex',
data () {
return {
begintime: '',
endtime: '',
start_time: '',
}
},
methods: {
setBeginTime () {
var _this = this;
var mintime = laydate.now(0, 'YYYY-MM-DD hh:mm:ss');
_this.$data.begintime = mintime;
_this.$data.endtime = mintime;
var begintiem_options = {
elem: '#begintime',
format: 'YYYY-MM-DD hh:mm:ss', // 分隔符可以任意定義
event: 'click', //觸發(fā)事件
istime: true, //是否開啟時間選擇
isclear: true, //是否顯示清空
issure: true, //是否顯示確認
festival: true, //是否顯示節(jié)日
min: mintime, //最小日期
max: '2099-12-31 23:59:59', //最大日期
start: mintime, //開始日期
fixed: true, //是否固定在可視區(qū)域
zIndex: 99999999, //css z-index
choose: function(dates) { // 選擇日期完畢的回調(diào)
endtime_options.start = dates;
endtime_options.min = dates;
_this.$data.begintime = dates;
_this.$data.endtime = dates;
}
};
var endtime_options = {
elem: '#endtime',
format: 'YYYY-MM-DD hh:mm:ss', // 分隔符可以任意定義
event: 'click', //觸發(fā)事件
istime: true, //是否開啟時間選擇
isclear: true, //是否顯示清空
issure: true, //是否顯示確認
festival: true, //是否顯示節(jié)日
min: _this.$data.begintime, //最小日期
max: '2099-12-31 23:59:59', //最大日期
start: _this.$data.begintime, //開始日期
fixed: true, //是否固定在可視區(qū)域
zIndex: 99999999, //css z-index
choose: function(dates) { // 選擇日期完畢的回調(diào)
// this.begintiem_options = dates;
}
};
laydate(begintiem_options);
laydate(endtime_options);
},
initPage () {
var _this = this;
_this.setBeginTime();
},
},
mounted () {
this.initPage();
},
}
</script>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
validate?注冊頁的表單數(shù)據(jù)校驗實現(xiàn)詳解
這篇文章主要為大家介紹了validate?注冊頁的表單數(shù)據(jù)校驗實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
關(guān)于vue3 vuex4 store的響應(yīng)式取值問題解決
這篇文章主要介紹了vue3 vuex4 store的響應(yīng)式取值問題,在實際生活中遇到這樣一個問題:在頁面中點擊按鈕,數(shù)量增加,值是存在store中的,點擊事件值沒變,如何解決這個問題,本文給大家分享解決方法,需要的朋友可以參考下2022-08-08

