如何将数据从 vuejs 中的父组件传递到 quasar tiptap 编辑器?
How to pass data into quasar tiptap editor from parent component in vuejs?
我有点困惑如何在我的应用程序中将其用作组件。具体来说,我很困惑如何正确地将数据从我的父组件传递到 https://github.com/donotebase/quasar-tiptap 中的 <quasar-editor />
。
我下面的代码会将父组件中 episode.keyLessons
的数据传递到编辑器中。但是,当我尝试在编辑器中键入时,空格键没有注册。如果光标在顶部的 <h1>
部分并按下任何键,光标会立即跳到编辑器的 <p>
部分,而无需我单击那里。
我做错了什么?
我尝试过的:
Editor.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
<template>
<Editor v-model="episode.keyLessons" @update="update" />
</template>
<script>
data: () => ({
episode: {
keyLessons: null,
}
}),
methods: {
update(value) {
this.episode.keyLessons = value;
},
}
</script>
当您键入某些内容时,您同时也在重置内容,这可能是出现奇怪行为的原因。最好让 tiptap 处理更新,并且仅在内容(值)从外部(父组件)更改时才设置 tiptap 的内容。在大多数情况下,您只想最初设置内容。
我建议您这样做:
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");
}
};
我有点困惑如何在我的应用程序中将其用作组件。具体来说,我很困惑如何正确地将数据从我的父组件传递到 https://github.com/donotebase/quasar-tiptap 中的 <quasar-editor />
。
我下面的代码会将父组件中 episode.keyLessons
的数据传递到编辑器中。但是,当我尝试在编辑器中键入时,空格键没有注册。如果光标在顶部的 <h1>
部分并按下任何键,光标会立即跳到编辑器的 <p>
部分,而无需我单击那里。
我做错了什么?
我尝试过的:
Editor.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
<template>
<Editor v-model="episode.keyLessons" @update="update" />
</template>
<script>
data: () => ({
episode: {
keyLessons: null,
}
}),
methods: {
update(value) {
this.episode.keyLessons = value;
},
}
</script>
当您键入某些内容时,您同时也在重置内容,这可能是出现奇怪行为的原因。最好让 tiptap 处理更新,并且仅在内容(值)从外部(父组件)更改时才设置 tiptap 的内容。在大多数情况下,您只想最初设置内容。
我建议您这样做:
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");
}
};