简体   繁体   English

如何将数据从 vuejs 中的父组件传递到 quasar tiptap 编辑器?

[英]How to pass data into quasar tiptap editor from parent component in vuejs?

I'm a little confused how to use this as a component in my application.我有点困惑如何在我的应用程序中使用它作为组件。 Specifically, I am confused how to properly pass data from my parent component into the <quasar-editor /> from https://github.com/donotebase/quasar-tiptap .具体来说,我很困惑如何从我的父组件正确地将数据从https://github.com/donotebase/quasar-tiptap传递到<quasar-editor />

My code below will pass the data from episode.keyLessons in my parent component into the editor.我下面的代码会将来自我父组件中的episode.keyLessons的数据传递到编辑器中。 However, when I try and type in the editor, the spacebar key does not register.但是,当我尝试在编辑器中输入时,空格键不会注册。 If the cursor is in the <h1> section at the top and any key is pressed, the cursor immediately jumps down to the <p> section of the editor without me clicking there.如果光标位于顶部的<h1>部分并且按下任意键,光标会立即跳到编辑器的<p>部分,而我无需单击该部分。

What am I doing wrong?我究竟做错了什么?

What I have tried:我尝试过的:

Editor.vue编辑器.vue

<template>
  <div>
    <quasar-tiptap v-bind="options" @update="onUpdate" />
  </div>
</template>

<script>
import { QuasarTiptap, RecommendedExtensions } from "quasar-tiptap";
import "quasar-tiptap/lib/index.css";

export default {
  name: "Editor",
  props: ['value'],
  data() {
    return {
      options: {
        content: '',
        editable: true,
        extensions: [
          ...RecommendedExtensions
          // other extenstions
          // name string, or custom extension
        ],
        toolbar: [
          "add-more",
          "separator",
          "bold",
          // other toolbar buttons
          // name string
        ]
      },
      json: "",
      html: ""
    };
  },
  components: {
    QuasarTiptap
  },
  methods: {
    onUpdate({ getJSON, getHTML }) {
      this.json = getJSON();
      this.html = getHTML();
      this.$emit('update', this.html)
    }
  },
  watch: {
    value: {
      handler(newVal, oldVal) {
        this.options.content = newVal;
        console.log(`value changed: ${newVal}`);
      },
      immediate: true
    }
  },
  mounted() {
    // set language dynamically
    this.$o.lang.set("en-us");
  }
};
</script>

Parent.vue父.vue

<template> 
   <Editor v-model="episode.keyLessons" @update="update" />
</template>

<script>
data: () => ({
   episode: {
      keyLessons: null,
   }
}),
methods: {
   update(value) {
         this.episode.keyLessons = value;
       },
}
</script>

When you type something you are also resetting the content and that is probably the cause of the weird behavior.当您输入内容时,您也在重置内容,这可能是导致奇怪行为的原因。 It's best to let tiptap handle the updates and only set the content of tiptap if the content (value) changes from the outside (parent component).如果内容(值)从外部(父组件)更改,最好让 tiptap 处理更新,并且仅设置 tiptap 的内容。 In most cases you only want to set the content initially.在大多数情况下,您最初只想设置内容。

Here's how I recommend that you do it:以下是我建议您这样做的方法:

export default {
  name: "Editor",
  props: ['value'],
  data() {
    return {
      valueChangedFromEditor: false,
      options: {
        content: '',
        editable: true,
        extensions: [
          ...RecommendedExtensions
          // other extenstions
          // name string, or custom extension
        ],
        toolbar: [
          "add-more",
          "separator",
          "bold",
          // other toolbar buttons
          // name string
        ]
      },
      json: "",
      html: ""
    };
  },
  components: {
    QuasarTiptap
  },
  methods: {
    onUpdate({ getJSON, getHTML }) {
      this.valueChangedFromEditor = true;
      this.json = getJSON();
      this.html = getHTML();
      this.$emit('update', this.html)
    }
  },
  watch: {
    value: {
      handler(newVal, oldVal) {
        // Only update if the value changed from parent component.
        if (!this.valueChangedFromEditor) {
          this.options.content = newVal;
          console.log(`value changed: ${newVal}`);
        }
        this.valueChangedFromEditor = false;
      },
      immediate: true
    }
  },
  mounted() {
    // set language dynamically
    this.$o.lang.set("en-us");
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM