vue自定义指令elementui日期组件手动输入

html

bash 复制代码
 <el-form-item label="开始时间" prop="beginTime">
            <!-- :picker-options="pickerOptions" -->
            <el-date-picker
              v-model="formData.beginTime"
              v-elDateFormat
              editable
              value-format="yyyyMMdd"
              type="date"
              placeholder="请选择日期"
              clearable
              style="width: 180px"
            />
</el-form-item>

scipt

bash 复制代码
  // 自定义指令
  directives: {
    elDateFormat: {
      inserted: function(el, binding, vnode) {
        const { value: _obj } = binding
        const { context: that, data } = vnode
        const { expression: key } = data.model
        if (that && that._isVue) {
          const $this = el.children[0]
          $this.onchange = function() {
            let value = $this.value // 中文日期
            console.log(value)
            if (value.match(/\d{1,}/g) && value.length == 8) {
              value = value.replace(/^(\d{4})\D*(\d{1,2})\D*(\d{1,2})\D*/, '$1-$2-$3') // 格式化输入格式
              const time = value && value.constructor == String ? new Date(value) : value // 转换时间格式
              let keys = key.split('.') // 自定义赋值
              if (_obj) {
                const { keys: keyList } = _obj
                keys = keyList
              }
              if (keys && keys.length >= 2) {
                const [key1, key2, key3, key4] = keys
                if (key4) {
                  that[key1][key2][key3][key4] = time
                } else if (key3) {
                  that[key1][key2][key3] = time
                } else {
                  that[key1][key2] = time
                }
              } else {
                that[key] = time
              }
            }
          }
        }
      }
    }
  },
相关推荐
座山雕~几秒前
html 和css基础常用的标签和样式
前端·css·html
灰小猿1 小时前
Spring前后端分离项目时间格式转换问题全局配置解决
java·前端·后端·spring·spring cloud
im_AMBER1 小时前
React 16
前端·笔记·学习·react.js·前端框架
02苏_1 小时前
ES6模板字符串
前端·ecmascript·es6
excel1 小时前
⚙️ 一次性警告机制的实现:warnOnce 源码深度解析
前端
excel1 小时前
Vue SFC 样式编译核心机制详解:compileStyle 与 PostCSS 管线设计
前端
excel1 小时前
🧩 使用 Babel + MagicString 实现动态重写 export default 的通用方案
前端
excel1 小时前
Vue SFC 编译器主导出文件解析:模块组织与设计哲学
前端
excel1 小时前
深度解析:Vue SFC 模板编译器核心实现 (compileTemplate)
前端
excel1 小时前
Vue SFC 解析器源码深度解析:从结构设计到源码映射
前端