0👍
I suspect it has to do with the DOM manipulation done in quill.js
, which conflicts with Vue’s own management of the virtual DOM.
However, I was able to resolve the issue by implementing the following optimizations.
Optimizations
You can significantly reduce the duplication of code and have a single <quill-editor>
by conditionally setting store.re.body
/store.re.descr
based on props.description
:
<template>
<div id="text-editor" class="text-editor">
<quill-editor ref="quill" :modules="modules" :toolbar="toolbar" v-model:content="content" contentType="html" />
</div>
</template>
<script setup>
⋮
const props = defineProps({
description: Boolean,
})
👇
if (props.description) {
store.re.descr = ''
} else {
store.re.body = ''
}
watch(content, newValue => {
newContent = newValue
👇
if (props.description) {
store.re.descr = newValue
} else {
store.re.body = newValue
}
})
watch(
() => props.description ? store.re.descr : store.re.body, 👈
newValue => {⋯}
)
⋮
</script>
But this solution does not scale well if you later need to have the editor update more store properties (e.g., store.re.title
and store.re.subtitle
). A better solution would be to make the editor update any given property via a v-model
implementation:
-
Define a
modelValue
string prop (viadefineProps
). -
Define an
update:modelValue
emit with a string value (viadefineEmits
). -
In the watch that sets
store.re.body
/store.re.descr
, emit theupdate:modelValue
event with new value. -
In the watch for
store.re.body
/store.re.descr
, change the watch’s source to beprops.modelValue
.
<script setup>
⋮
1️⃣
const props = defineProps({
modelValue: String,
})
2️⃣
const emit = defineEmits({
'update:modelValue': value => true,
})
watch(content, newValue => {
newContent = newValue
3️⃣
emit('update:modelValue', newValue)
})
watch(
() => props.modelValue, 4️⃣
newValue => {⋯}
)
⋮
</script>
Then in the parent component, use v-model
to bind store.re.body
/store.re.descr
:
<text-editor v-model="store.re.descr" />
<text-editor v-model="store.re.body" />
For some reason, setting store.re.body
and store.re.descr
in the parent component does not update the v-model
in this case, but you can workaround that by using a computed prop. To make the changes to store.re.body
reactive in this example:
<template>
<text-editor v-model="body" />
</template>
<script setup>
import { computed } from 'vue'
const body = computed({
get: () => store.re.body,
set: v => store.re.body = v,
})
</script>
- [Vuejs]-Get a blank white screen with only app.vue items but not other component
- [Vuejs]-How to import vForm globally in index.js using Vue js 3