在 Astro 中集成
请先阅读安装和基本使用。以下示例通过 Astro 将 Univer Bases 挂载到具有明确高度的容器中。
使用普通的 <script>,在浏览器中直接初始化编辑器。首次加载时执行初始化,使用 ClientRouter 时通过 astro:page-load 重新挂载,在 astro:before-swap 中销毁旧实例。不要在 Astro frontmatter 中导入编辑器。详见 Astro 导航事件。
集成步骤
- 在
mount()/astro:page-load中初始化 Univer - 在
astro:before-swap中销毁 Univer
示例代码
Astro
<div id="univer-bases" style="height: 600px"></div><script> import { UniverBasesPlugin } from '@univerjs-pro/bases' import { UniverBasesUIPlugin } from '@univerjs-pro/bases-ui' import BasesUIzhCN from '@univerjs-pro/bases-ui/locale/zh-CN' import BaseszhCN from '@univerjs-pro/bases/locale/zh-CN' import { UniverProFormulaEnginePlugin } from '@univerjs-pro/engine-formula' import { UniverLicensePlugin } from '@univerjs-pro/license' import { LocaleType, mergeLocales, Univer, UniverInstanceType } from '@univerjs/core' import DesignzhCN from '@univerjs/design/locale/zh-CN' import { UniverRenderEnginePlugin } from '@univerjs/engine-render' import { UniverUIPlugin } from '@univerjs/ui' import UIzhCN from '@univerjs/ui/locale/zh-CN' import '@univerjs/design/lib/index.css' import '@univerjs/ui/lib/index.css' import '@univerjs-pro/bases-ui/lib/index.css' let univerInstance: Univer | undefined function mount() { const container = document.getElementById('univer-bases') if (!container || univerInstance) return const univer = new Univer({ locale: LocaleType.ZH_CN, locales: { [LocaleType.ZH_CN]: mergeLocales( DesignzhCN, UIzhCN, BaseszhCN, BasesUIzhCN, ), }, }) univer.registerPlugin(UniverLicensePlugin, { license: 'your license.txt', }) univer.registerPlugin(UniverRenderEnginePlugin) univer.registerPlugin(UniverUIPlugin, { container }) univer.registerPlugin(UniverProFormulaEnginePlugin) univer.registerPlugin(UniverBasesPlugin) univer.registerPlugin(UniverBasesUIPlugin) univer.createUnit(UniverInstanceType.UNIVER_BASE, {}) univerInstance = univer } function dispose() { univerInstance?.dispose() univerInstance = undefined } mount() document.addEventListener('astro:page-load', mount) document.addEventListener('astro:before-swap', dispose)</script>你觉得这篇文档如何?