在 Astro 中集成

请先阅读安装和基本使用。以下示例通过 Astro 将 Univer Docs 挂载到具有明确高度的容器中。

使用普通的 <script>,在浏览器中直接初始化编辑器。首次加载时执行初始化,使用 ClientRouter 时通过 astro:page-load 重新挂载,在 astro:before-swap 中销毁旧实例。不要在 Astro frontmatter 中导入编辑器。详见 Astro 导航事件

集成步骤

  1. mount() / astro:page-load 中初始化 Univer
  2. astro:before-swap 中销毁 Univer

示例代码

Astro
<div id="univer-docs" style="height: 600px"></div><script>  import type { Univer } from '@univerjs/presets'  import { UniverDocsCorePreset } from '@univerjs/preset-docs-core'  import UniverPresetDocsCorezhCN from '@univerjs/preset-docs-core/locales/zh-CN'  import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'  import '@univerjs/preset-docs-core/lib/index.css'  let univerInstance: Univer | undefined  function mount() {    const container = document.getElementById('univer-docs')    if (!container || univerInstance) return    const { univer, univerAPI } = createUniver({      locale: LocaleType.ZH_CN,      locales: { [LocaleType.ZH_CN]: mergeLocales(UniverPresetDocsCorezhCN) },      presets: [UniverDocsCorePreset({ container })],    })    univerAPI.createDocument({})    univerInstance = univer  }  function dispose() {    univerInstance?.dispose()    univerInstance = undefined  }  mount()  document.addEventListener('astro:page-load', mount)  document.addEventListener('astro:before-swap', dispose)</script>

你觉得这篇文档如何?

© 2026 DreamNum Co., Ltd.