【问题标题】:Vuejs, computed property with setter freeze componentVuejs,带有设置器冻结组件的计算属性
【发布时间】:2020-08-30 10:51:35
【问题描述】:

我有一个带有tiptap 文本编辑器的组件。我使用带有 setter 的计算属性将数据绑定到编辑器。

<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>

当我快速输入大量文本时,我的组件会冻结。我使用计算属性是因为我必须获取一些当前文本,然后才能更改它。有人知道这种情况的最佳做法吗?我该如何解决这个问题?

【问题讨论】:

  • 如果您可以从存储中获取文本但仅在按下保存或编辑时才提交到存储。
  • @YJRB,是的,这是一种方式,但我尝试实现自动保存解决方案。

标签: javascript vue.js rich-text-editor computed-properties tiptap


【解决方案1】:

此类问题的常见解决方案是debounce 调用,这会延迟回调事件。例如,您可以使用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)
      }
    }
  }
}

【讨论】:

  • 它就像一个魅力!顺便说一句,我想到了计时器,可能今天会实现它,但你的回答太棒了 =)
猜你喜欢
  • 2019-11-12
  • 2020-03-27
  • 2020-06-19
  • 1970-01-01
  • 1970-01-01
  • 2021-01-14
  • 1970-01-01
  • 2019-03-31
  • 2017-08-23
相关资源
最近更新 更多