欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue使用laydate时间插件的方法

程序员文章站 2022-11-23 10:59:57
之前在做vue项目时使用iviewui库中的datepicker组件,发现datepicker使用起来比较麻烦,尤其是对时间精确度上的限制不尽人意,操作起来也比较繁琐,总之...

之前在做vue项目时使用iviewui库中的datepicker组件,发现datepicker使用起来比较麻烦,尤其是对时间精确度上的限制不尽人意,操作起来也比较繁琐,总之在处理一系列时间组件相互联动上存在一大堆问题,比如

vue使用laydate时间插件的方法

datepicker时间组件

时间精确到分,组件1的value等于组件2的最小值,组件2的vlaue等于组件3的最小值,。。。依次类推,如果是时间精确到日,这个组件没有任何问题,如果是精确到时分秒,这个组件的对时分秒的控制就不是那个灵敏了,在点击时分秒之后才能识别出对时分秒的限制,而这种用户体验不够友好。

所以,想到了以前用过的laydate时间插件

在vue中使用laydate

在vue组件中npm install laydate,然后在vue组件中直接引入:

import laydate from 'laydate'

然后在mounted中调用:

laydate.now();

发现chrome浏览器控制台中报错,laydate is not defined,然后把laydate.js放在静态资源里面引入:

import laydate from '../../static/js/laydate.js'

发现chrome浏览器控制台中依然是报错,laydate is not defined,然后在html入口文件中引入:

<script src="./src/static/js/laydate.js"></script>

发现chrome浏览器控制台中不报,laydate is not defined,但是又报另外一个错误:require is not defined,查看原码发现是因为laydate.js中在引用css样式表时未定义:require('./need/laydate.css');require('./skins/default/laydate.css'); 

最后使用laydate打包构建后的文件:dist/laydate/laydate.min.js,把laydate.min.js和css粘贴到src/static/路径下,注意js和css结构不要改变

<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出口,所以不能使用这种方式引用,应该使用:

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>

当然,如果项目中只是个别地方是使用到时间插件,建议采用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="">结束时间:</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', //触发事件
          istime: true, //是否开启时间选择
          isclear: true, //是否显示清空
          issure: true, //是否显示确认
          festival: true, //是否显示节日
          min: mintime, //最小日期
          max: '2099-12-31 23:59:59', //最大日期
          start: mintime, //开始日期
          fixed: true, //是否固定在可视区域
          zindex: 99999999, //css z-index
          choose: function(dates) { // 选择日期完毕的回调
            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', //触发事件
          istime: true, //是否开启时间选择
          isclear: true, //是否显示清空
          issure: true, //是否显示确认
          festival: true, //是否显示节日
          min: _this.$data.begintime, //最小日期
          max: '2099-12-31 23:59:59', //最大日期
          start: _this.$data.begintime, //开始日期
          fixed: true, //是否固定在可视区域
          zindex: 99999999, //css z-index
          choose: function(dates) { // 选择日期完毕的回调
            // this.begintiem_options = dates;
          }
        };
 
        laydate(begintiem_options);
        laydate(endtime_options);
        
      },
      initpage () {
        var _this = this;
        _this.setbegintime();
      },
    },
    mounted () {
      this.initpage();
    },
  }
</script>

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。