vue3/vue2中自定义指令不可输入小数点.

import {
    directive } from 'vue'

const noDecimal = {
   
  mounted(el) {
   
    el.addEventListener('keypress', (e) => {
   
      if (e.key === '.') {
   
        e.preventDefault()                          
      }
    })
  }
}

// 使用自定义指令
directive('no-decimal', noDecimal)
  • 使用 标签上添加 v-no-decimal
    <el-input type="text" v-model="value" v-no-decimal placeholder="请输入..." > </el-input>
v3 指令生命周期

在这里插入图片描述

v2指令生命周期 官网
vue2中mounted 替换成bind
  directives: {
   
    'no-decimal': {
   
      bind: (el) => {
   
        el.addEventListener('keypress', (event) => {
   
          if (event.key === '.') {
   
            event.preventDefault()
          }
        })
      }
    }
  },

在这里插入图片描述

相关推荐

  1. Vue2Vue3定义指令的写法

    2024-02-01 10:58:06       57 阅读
  2. vue3定义指令

    2024-02-01 10:58:06       65 阅读
  3. vue3定义指令

    2024-02-01 10:58:06       70 阅读
  4. VUE3 定义指令

    2024-02-01 10:58:06       43 阅读
  5. vue3定义指令

    2024-02-01 10:58:06       36 阅读
  6. Vue3.2 定义指令详解与实战

    2024-02-01 10:58:06       63 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-01 10:58:06       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-01 10:58:06       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-01 10:58:06       87 阅读
  4. Python语言-面向对象

    2024-02-01 10:58:06       96 阅读

热门阅读

  1. webpack优化配置-dll方式将第三方库独立打包

    2024-02-01 10:58:06       57 阅读
  2. Python学习笔记——类型注解基础知识

    2024-02-01 10:58:06       47 阅读
  3. linux 文本处理

    2024-02-01 10:58:06       55 阅读
  4. dubbo rpc序列化

    2024-02-01 10:58:06       52 阅读
  5. js动画形式

    2024-02-01 10:58:06       62 阅读
  6. c++二叉树

    2024-02-01 10:58:06       60 阅读
  7. C++ 预处理器

    2024-02-01 10:58:06       45 阅读