除去特殊字符和表情包的指令
import Vue from 'vue';
let findEle = (parent, type) => {
return parent.tagName.toLowerCase() === type ? parent : parent.querySelector(type)
}
const trigger = (el, type) => {
const e = document.createEvent('HTMLEvents')
e.initEvent(type, true, true)
el.dispatchEvent(e)
}
const emoji = Vue.directive('emoji', {
bind: function (el) {
// 正则规则可根据需求自定义
var regRule = /[`~!@#$%^&*()+=|{}':;',/\/\[\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|[\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/;
let $inp = findEle(el, 'input')
el.$inp = $inp
$inp.handle = function () {
let val = $inp.value
if (regRule.test(val)) {
Vue.prototype.$message({
type: 'warning',
message: '不能输入特殊字符或者表情包'
})
}
$inp.value = val.replace(regRule, '');
trigger($inp, 'input')
}
$inp.addEventListener('keyup', $inp.handle)
},
unbind: function (el) {
el.$inp.removeEventListener('keyup', el.$inp.handle)
},
})
export {emoji}