描述
在业务中通常遇到,搜索的输入框内容需要去除前后空格。最方便的处理方式就是使用自定义指令 话不多说,直接上代码
解决方案
import Vue from 'vue';
// input 输入限制前后空格
Vue.directive('no-space', {
inserted(el) {
el.addEventListener('input', function (event) {
event.target.value = event.target.value.trim();
event.target.dispatchEvent(new Event('input'));
});
el.addEventListener('blur', function (event) {
event.target.dispatchEvent(new Event('input'));
});
}
});