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()}})}}},