
本文旨在解决ckeditor 5在线构建器自定义版本在react应用中集成时遇到的`typeerror: cannot read properties of undefined (reading 'create')`错误。该问题通常源于`watchdog`功能冲突,因为react集成包已内置此功能。解决方案是通过`watchdogconfig`属性配置或禁用`watchdog`,确保编辑器正确渲染。
在使用CKEditor 5构建富文本编辑器时,尤其是在React环境中集成通过在线构建器(Online Builder)生成的自定义版本时,开发者可能会遇到编辑器无法正常渲染并抛出TypeError: Cannot read properties of undefined (reading 'create')的错误。尽管CKEditor 5的预构建版本(如classic或balloon)能够顺利运行,但自定义构建版本却可能出现此问题。本文将深入分析此错误的原因并提供详细的解决方案。
当集成自定义构建的CKEditor 5到React组件中时,控制台可能会出现类似如下的错误信息:
ckeditor.tsx:156 TypeError: Cannot read properties of undefined (reading 'create')
at to._createEditor (ckeditor.tsx:169:1)
at Vr._creator (ckeditor.tsx:133:1)
at editorwatchdog.js:115:1
at async to._initializeEditor (ckeditor.tsx:151:1)
at async to.componentDidMount (ckeditor.tsx:102:1)此错误通常发生在编辑器初始化阶段,表明Editor对象(或其内部的某个关键方法)在被调用时为undefined,导致无法执行create操作。这暗示了编辑器核心模块的加载或初始化存在问题。
初步排查发现,项目结构和代码导入方式似乎正确,例如:
import { CKEditor } from '@ckeditor/ckeditor5-react';
import { Editor } from 'ckeditor5-custom-build/build/ckeditor';
const MyEditorComponent = () => {
return (
<CKEditor
editor={Editor}
data="<p>Hello from CKEditor 5!</p>"
onReady={editor => {
console.log('Editor is ready to use!', editor);
}}
// ... 其他事件处理
/>
);
};并且package.json中的依赖也显示了正确的版本:
"@ckeditor/ckeditor5-build-balloon": "^38.0.1", "@ckeditor/ckeditor5-react": "^6.0.0", "ckeditor5-custom-build": "file:./ckeditor5", // 指向本地自定义构建
问题的核心在于CKEditor 5的watchdog(看门狗)功能与@ckeditor/ckeditor5-react集成包之间的潜在冲突。
根据CKEditor 5的官方文档说明:
"If you want to use the CKEditor 5 online builder, make sure that the watchdog feature is not selected. The React integration comes with the watchdog feature already integrated into the core."
这意味着:
解决此问题主要有两种方法,推荐第二种(重新构建编辑器)。
您可以通过在<CKEditor>组件上添加watchdogConfig属性来显式地配置或调整watchdog的行为,以避免冲突。
import { CKEditor } from '@ckeditor/ckeditor5-react';
import { Editor } from 'ckeditor5-custom-build/build/ckeditor';
const DashboardPage = () => {
return (
<div className='card'>
<div className='card-body'>
<div className='mx-auto'>
<CKEditor
editor={Editor}
data="<p>Hello from CKEditor 5!</p>"
// 添加 watchdogConfig 属性
watchdogConfig={ { crashNumberLimit: 10 } } // 示例配置,可以根据需求调整
onReady={editor => {
console.log('Editor is ready to use!', editor);
}}
onChange={(event, editor) => {
// const data = editor.getData();
// console.log({ event, editor, data });
}}
onBlur={(event, editor) => {
console.log('Blur.', editor);
}}
onFocus={(event, editor) => {
console.log('Focus.', editor);
}}
/>
</div>
</div>
</div>
);
};
export default DashboardPage;watchdogConfig={ { crashNumberLimit: 10 } } 这是一个示例配置,它允许您控制watchdog在编辑器崩溃多少次后停止尝试恢复。通过提供这个配置,即使自定义构建中包含了watchdog,React集成包也能更好地协调其行为,从而避免初始化冲突。
最根本且推荐的解决方案是确保您的自定义构建本身不包含watchdog功能,因为React集成包已经处理了这一点。
import { CKEditor } from '@ckeditor/ckeditor5-react';
import { Editor } from 'ckeditor5-custom-build/build/ckeditor';
const DashboardPage = () => {
return (
<div className='card'>
<div className='card-body'>
<div className='mx-auto'>
<CKEditor
editor={Editor}
data="<p>Hello from CKEditor 5!</p>"
// 重新构建后,无需 watchdogConfig
onReady={editor => {
console.log('Editor is ready to use!', editor);
}}
// ... 其他事件处理
/>
</div>
</div>
</div>
);
};
export default DashboardPage;当CKEditor 5自定义构建在React应用中遇到TypeError: Cannot read properties of undefined (reading 'create')错误时,根本原因往往是watchdog功能在自定义构建和@ckeditor/ckeditor5-react集成包之间产生了冲突。通过在React组件中配置watchdogConfig属性,或更推荐地,在CKEditor 5在线构建器中排除watchdog功能,可以有效解决此问题,确保编辑器在React应用中顺利渲染和运行。遵循官方指导并理解集成机制是实现无缝集成的关键。
以上就是CKEditor 5 自定义构建在React应用中渲染失败的调试与解决的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号