博客
关于我
一个车牌输入组件(vue)
阅读量:386 次
发布时间:2019-03-05

本文共 1900 字,大约阅读时间需要 6 分钟。

基于Vue的简单车牌输入组件实现

功能展示

该车牌输入组件支持多种车牌结构输入,包括省份代码、数字和其他特殊字符。组件采用点击输入的方式,支持字符的逐步输入和删除操作。

代码结构

## 事件处理```javascriptclickInput(type) { this.plateInput.input.type = type; this.plateInput.input.dialogVisible = true;}hiddenKeybord() { this.plateInput.input.dialogVisible = false;}enterWord() { this.plateInput.input.dialogVisible = false;}

方法实现

clickKeyboard(val) {  if (this.plateInput.input.type === 'p2' && parseInt(val) >= 0 && parseInt(val) <= 9) return;  this.methods('clickKeyboard', val);  this.methods('setPlateNumber');  this.methods('setDirectIssuedPlateNumber');}clickDelete() {  this.plateInput.input.value[this.plateInput.input.type] = undefined;  let nu = parseInt(this.plateInput.input.type.split('p')[1]) - 1;  if (nu >= 0) {    this.plateInput.input.value['p' + nu] = undefined;  }  let type = this.plateInput.input.type.split('p')[1];  if (type !== '1') {    this.plateInput.input.type = 'p' + (parseInt(type) - 1);  }}

数据模型

export let model = {  currentPlate: undefined,  plateInput: {    input: {      value: {        p1: '桂',        p2: 'B',        p3: 2,        p4: 2,        p5: 2,        p6: 2,        p7: 2,        p8: 0      },      type: 'p1',      dialogVisible: false    }  },  Keyboard: {    province: ['京', '津', '冀', '晋', '蒙', '辽', '吉', '黑', '沪', '苏', '浙', '皖', '闽', '赣', '鲁', '豫', '鄂', '湘', '粤', '桂', '琼', '渝', '川', '贵', '云', '藏', '陕', '甘', '青', '宁', '新', '台', '港', '澳', '使', '领', '警', '学'],    number: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '学', '港', '澳']  }}

代码优化建议

  • 去除所有无效的空白div标签
  • 合并重复的样式表
  • 使用更优化的变量命名
  • 增加注释,提升代码可读性
  • 优化键盘事件响应逻辑
  • 增加字符验证功能,避免非法字符输入
  • 转载地址:http://ksgwz.baihongyu.com/

    你可能感兴趣的文章
    Python - 轻松将文本文件内容转换为字典值/键
    查看>>
    Python - 递归和列表
    查看>>
    Python 3 读取ini文件
    查看>>
    Python 3.0 使用 turtle.onclick
    查看>>
    Python 3.10 明年发布,看看都有哪些新特性?
    查看>>
    python 3.10上安装pyqt5
    查看>>
    Python 3.12 正式发布了!
    查看>>
    Python 3.2 中的蛮力脚本
    查看>>
    Python 3.4 多处理递归 Pool.map()
    查看>>
    Python 3.4:未知格式代码“x“
    查看>>
    Python 3.5、ldap3 和 modify_password()
    查看>>
    python 3.6.8 升级至3.9版本升级
    查看>>
    Python 3.9 到 Python 3.12 的发展历程与区别
    查看>>
    python 32位和64位的区别在哪
    查看>>
    Python 3:何时使用 dict,何时使用元组列表?
    查看>>
    Python 3d 绘图 - 轴居中
    查看>>
    python ==》 字典
    查看>>
    python anaconda 安装使用
    查看>>
    python and或or 当参数传递的时候的用法
    查看>>
    Python append() 与列表上的 + 运算符,为什么这些会给出不同的结果?
    查看>>