[英]Vuejs, computed property with setter freeze component
I have a component with tiptap text editor.我有一个带有 tiptap 文本编辑器的组件。 I use a computed property with a setter to bind data to the editor.我使用带有设置器的计算属性将数据绑定到编辑器。
<template>
<tiptap model="editNoteContent" extensions" />
</template>
<script>
export default {
computed: {
editNoteContent: {
get() {
return this.$store.state.Notes.currentNote.text;
},
set(text) {
this.$store.commit("Notes/updateCurrentNoteText", text);
},
}
},
}
</script>
When I type a lot of text fast, my component freezes.当我快速输入大量文本时,我的组件会冻结。 I use a computed property because I have to get some current text, and only then change it.我使用计算属性,因为我必须获取一些当前文本,然后才更改它。 Does somebody know best practice for situation like this?有人知道这种情况的最佳做法吗? How can I solve this issue?我该如何解决这个问题?
A common solution to this type of problem is to debounce the call, which delays the callback event.此类问题的常见解决方案是对调用进行去抖动,这会延迟回调事件。 For example, you could debounce the mutation by using clearTimeout()
to cancel any pending calls, and then setTimeout()
to delay the $store.commit()
call:例如,您可以通过使用clearTimeout()
来取消任何挂起的调用,然后使用setTimeout()
来延迟$store.commit()
调用来消除突变:
export default {
computed: {
editNoteContent: {
get() { /*...*/ },
set(text) {
// cancel pending commits, if any
clearTimeout(this._timer)
// commit newest text after 300ms
this._timer = setTimeout(() => {
this.$store.commit("Notes/updateCurrentNoteText", text)
}, 300)
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.