在 Astro 中集成

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

使用普通的 <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-pdfs" style="height: 600px"></div><script>  import { UniverLicensePlugin } from '@univerjs-pro/license'  import { UniverPdfsPlugin } from '@univerjs-pro/pdfs'  import { UniverPdfsUIPlugin } from '@univerjs-pro/pdfs-ui'  import PdfsUIzhCN from '@univerjs-pro/pdfs-ui/locale/zh-CN'  import { LocaleType, mergeLocales, Univer } from '@univerjs/core'  import { FUniver } from '@univerjs/core/facade'  import DesignzhCN from '@univerjs/design/locale/zh-CN'  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/pdfs-ui/lib/index.css'  import '@univerjs-pro/pdfs/facade'  let univerInstance: Univer | undefined  function mount() {    const container = document.getElementById('univer-pdfs')    if (!container || univerInstance) return    const univer = new Univer({      locale: LocaleType.ZH_CN,      locales: {        [LocaleType.ZH_CN]: mergeLocales(          DesignzhCN,          UIzhCN,          PdfsUIzhCN,        ),      },    })    univer.registerPlugin(UniverLicensePlugin, {      license: 'your license.txt',    })    univer.registerPlugin(UniverUIPlugin, { container })    univer.registerPlugin(UniverPdfsPlugin)    univer.registerPlugin(UniverPdfsUIPlugin)    const univerAPI = FUniver.newAPI(univer)    univerAPI.createPdf({ name: 'Untitled PDF' })    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.