简体   繁体   English

Vuejs,带有setter冻结组件的计算属性

[英]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.

相关问题 已分配计算属性,但它没有设置器 - 切换组件 - Computed property was assigned to but it has no setter - a toggle component 如果计算属性vuejs中没有任何更改,如何将旧值传递给setter? - How to pass old value to setter if nothing is changed in computed property vuejs? 计算属性设置器导致 vuejs / javascript 在日期选择器焦点上崩溃浏览器 - computed property setter cause vuejs / javascript to crash the browser on the datepicker focus Ember 3计算的属性获取器设置器 - Ember 3 computed property getter setter Vuejs - 基于其他计算属性的计算属性 - Vuejs - computed property based on other computed property 将值分配给计算属性 vuejs - assigning values to a computed property vuejs 带有 Lodash Debounce 的 VueJS 计算属性 - VueJS Computed Property with Lodash Debounce VueJs 使计算属性具有反应性 - VueJs make computed property reactive VueJs / ChartJs-仅在我单击Vue Dev Tools中的组件时才呈现单个文件组件的计算属性 - VueJs/ChartJs - single file component computed property only renders when I click the component in Vue Dev Tools 如何在将 element-ui 与 VueJs 一起使用时在控制台中消除此错误“计算属性 validateState 已分配给但它没有设置器” - How do I get rid of this error "Computed property validateState was assigned to but it has no setter" in the console while using element-ui with VueJs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM