vue.js - 如何在 Vue 2.6 中 @paste 事件后拦截或清除 v-model 值
问题描述
我对输入字段上的 v-model 和 @paste 事件有疑问。
当我复制某些内容并将其粘贴到输入字段时,它也会在输入字段中显示复制的值。
我想防止这种情况。
我创建了一个简单的 JsFiddle Todo App 来显示问题。
https://jsfiddle.net/k12drcqn/1/
onPaste: function() {
let clipped = event.clipboardData.getData('text').split("\n");
clipped.forEach(item => {
this.todos.push({
text: item, done: false
})
})
// is not clearing the v-model: todo
this.todo = ''
}
例如,如果您将这样的内容复制到输入字段中:
- 任务1
- 任务2
- 任务3
这些任务将被添加到列表中,但也会显示在输入字段中。是否有可能不在输入字段中显示粘贴的任务?
解决方案
onPaste: function() {
let clipped = event.clipboardData.getData('text').split("\n");
clipped.forEach(item => {
this.todos.push({
text: item, done: false
})
})
// instead of this
// this.todo = ''
// make this
setTimeout(() => {
this.todo = ''
}, 0);
}
this.todo = ''
我认为同步调用时文本会保留在输入中。
推荐阅读
- javascript - 我是否正确理解了这个 Javascript 函数?
- javascript - 我在 Formik 中使用 axios 发布请求,并返回 400 错误
- google-chrome - 我想限制在 chromium 中加载的页面
- wordpress - 如何在 Wordpress 画廊中放置图像
- vb.net - 如何在 Visual Basic 中将 Json 字符串转换为 Json 数组
- javascript - 如何为 HTML 元素的 css 类之一设置超时?
- shopify - 使用新的 fileDelete 从管理页面删除文件时出现“无效 ID”
- radio-button - Unqork - 单选按钮样式
- android - 如何在片段 android studio 中添加 Searchview?
- python - 仅使用 BeautifulSoup 从测量网站获取数据