【发布时间】:2020-02-23 16:50:11
【问题描述】:
我正在尝试实现一个指令,该指令将修剪模糊事件的输入值:
import { DirectiveOptions } from "vue";
const Autotrim: DirectiveOptions = {
inserted(el) {
if(!(el instanceof HTMLInputElement) && !(el instanceof HTMLTextAreaElement)) {
throw 'Cannot apply v-autotrim directive to a non-input element!';
}
el.addEventListener('blur', () => {
if(el.value)
el.value = el.value.trim();
});
}
};
输入已更新,但模型中的绑定值未更新,并且在组件中的其他位置发生任何更改后,它会恢复为未修剪状态。
确保模型也得到更新的正确方法是什么?
编辑这里有一个codepen链接可以尝试: https://codepen.io/impworks/pen/mddMPyx
【问题讨论】:
标签: vue.js dom-events onblur vue-directives