宝哥软件园

vue使用laydate time插件的方法

编辑:宝哥软件园 来源:互联网 时间:2021-08-28

之前在做vue项目的时候,我们在iviewUI库中使用了DatePicker组件,发现DatePicker使用起来比较麻烦,尤其是对时间精度的限制不尽人意,操作起来比较繁琐。简而言之,在处理一系列时间成分的相互作用时有很多问题,例如

日期选择器时间组件

时间精确到分钟,分量1的值等于分量2的最小值,分量2的vlaue等于分量3的最小值。以此类推,如果时间精确到当天,这个组件没有问题;如果是精确到时间、分、秒的话,这个组件对时间、分、秒的控制就没有那么灵敏了,时间、分、秒的限制只有点击后才能识别,但是这种用户体验不够友好。

所以,我想到了之前使用的laydate time插件

在vue中使用laydate

在vue组件中,npm安装laydate,然后直接介绍:

从“laydate”导入laydate,然后装入调用:

laydate . now();在chrome浏览器控制台发现一个错误,laydate没有定义,然后把laydate.js放到静态资源中介绍:

从导入布局日期././static/js/laydate.js '发现chrome浏览器控制台仍然报告了一个错误,laydate没有定义,然后介绍:

脚本src=' http:/src/static/js/laydate . js '/脚本发现chrome browser console未报告,lay date未定义,但报告了另一个错误:require未定义,找到原始代码是因为在laydate.js: require('中引用css样式表时未定义。/need/laydate.css. require('。/skins/default/laydate . CSS ');

最后用laydate封装构造好的文件:dist/laydate/laydate.min.js,在src/static/path中粘贴laydate.min.js和css,注意js和css的结构。

脚本src=' http:/src/static/laydate/laydate . min . js '/脚本随后在chrome浏览器中成功打印laydate.now()的值:2018年10月21日

然后通过导入导入laydate.min.js:

从导入布局日期././static/laydate/laydate . min . js ' chrome控制台中有一个错误:laydate.now不是函数

原因是laydate.min.js直接在window上注册laydate对象,没有导出默认laydate导出,所以不能这样引用,应该是:

'导入后'././static/laydate.min.js '被引用,laydate.now()在chrome中成功打印。2018-10-21

或者也可以使用cdn加速将laydate.min.js引入条目文件index.html:

script src=' http :https://cdn . jsdeliver . net/NPM/[email protected]/dist/laydate . min . js '/script当然,如果项目中只有少数地方使用time插件,建议通过导入的方式引入。

vue组件中laydate.min.js时间插件的使用

模板div id=' LayDateIndex ' div class=' input-item '标签为=' '请选择时间:/标签输入类型=“文本”名称='houseChangeTime '占位符='请选择日期class=' form-control ' onclick=' lay date({ istime : true,format : ' YYYY/MM/DD hh :MM 3360s ' })'//div class=' input-item '标签为=' '开始时间:/标签输入类型=' text ' name=' id=' begintime ' placeholder='请选择日期/div class=' input-item '标签为=' '结束时间:/标签输入类型=' text ' name=' id=' end time '占位符='请选择日期/div /div/templatescript import '././资产/js/铺设日期。量滴js ' export default { name : ' laydate index ',data () { return { begintime: ' ',endtime: ' ',start_time: ' ',},methods : { setBeginTime(){ var _ this=this;var mintime=laydate.now(0,' YYYY-MM-DD hh :MM 3360s ');_这个$数据。开始时间=最小时间;_这个$数据。结束时间=最小时间;var beginitem _ options={ elem : ' #开始时间',格式: ' YYYY-MM-DD hh :MM :s ',//分隔符可以任意定义事件:”单击",//触发事件istime: true,//是否开启时间选择isclear: true,//是否显示清空issure: true,//是否显示确认festival: true,//是否显示节日min: mintime,//最小日期最大: ' 2099-12-31 23:59:59 ',//最大日期start: mintime,//开始日期fixed: true,//是否固定在可视区域zIndex: 99999999,//css z-index选择:函数(日期){ //选择日期完毕的回调end time _ options . start=datesendtime _ options . min=date _这个$ data.begintime=dates _这个$数据。结束时间=日期;} };var end time _ options={ elem : ' # end time ',格式: ' YYYY-MM-DD hh :MM 3360s ',//分隔符可以任意定义事件:”单击",//触发事件istime: true,//是否开启时间选择isclear: true,//是否显示清空issure: true,//是否显示确认festival: true,//是否显示节日min: _这个$data.begintime,//最小日期最大: ' 2099-12-31 23:59:59 ',//最大日期从今天开始:$data.begintime,//开始日期fixed: true,//是否固定在可视区域zIndex: 99999999,//css z-index选择:函数(日期){ //选择日期完毕的回调//这个。beginitem _ options=date} };放置日期(开始项目_选项);铺设日期(结束时间_选项);},init page(){ var _ this=this;_这个。setbegintime();},},mounted(){ this。init page();},}/脚本以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

更多资讯
游戏推荐
更多+