省流
script:
javascript
directives: {
regexp: {
inserted: (el, binding, vnode) => {
let composition = false
const formatValue = function (e) {
if (composition) return
// vnode.componentInstance组件实例
vnode.componentInstance.$emit('input', e.target.value.replace(/[^\u4e00-\u9fa5a-zA-Z0-9/]/g, ''))
}
el.oninput = formatValue
// 请务必使用addEventListenner,不信邪你试试el.oncompositionstart
el.addEventListener('compositionstart', () => { composition = true })
el.addEventListener('compositionend', e => {
composition = false
formatValue(e)
})
}
}
}
template:
html
<el-input v-regexp />