From 3ca8af262e06bf9c240233972fdc2e2373bbd0d1 Mon Sep 17 00:00:00 2001 From: wx <2636507191@qq.com> Date: Sat, 9 May 2026 09:14:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(editor):=20=E5=AF=8C=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E5=99=A8=E5=88=9D=E5=A7=8B=E5=8C=96=20race?= =?UTF-8?q?=20=E5=AF=BC=E8=87=B4=E6=AD=A3=E6=96=87=E4=B8=8D=E6=98=BE?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit useEditor 异步创建 vs loadItem 异步加载的 race condition: - mount 时 useEditor 用 props.modelValue='' 初始化 - watch modelValue 触发时 editor.value 还是 null 被跳过 - editor 实例化后 watch 不会再跑,最终内容为空 修复:增加 watch editor 实例的逻辑,编辑器创建后立即同步当前 modelValue。 影响范围:articles/[id]、blogs/[id] 编辑页(共用同一组件)。 Co-Authored-By: Claude Opus 4.7 (1M context) --- components/admin/RichTextEditor.vue | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/components/admin/RichTextEditor.vue b/components/admin/RichTextEditor.vue index 606f4ad..8b426a1 100644 --- a/components/admin/RichTextEditor.vue +++ b/components/admin/RichTextEditor.vue @@ -74,6 +74,14 @@ watch(() => props.modelValue, (val) => { if (val !== current) editor.value.commands.setContent(val || '', false) }) +// editor 实例异步创建,若先于 modelValue 异步加载完成, watch modelValue 时 editor 还是 null 会跳过, 导致内容空白 +watch(editor, (ed) => { + if (!ed) return + if (props.modelValue && ed.getHTML() !== props.modelValue) { + ed.commands.setContent(props.modelValue, false) + } +}, { immediate: true }) + onBeforeUnmount(() => editor.value?.destroy()) function btn(active) { return { 'toolbar-btn': true, active } }