在 Astro 中整合

請先閱讀安裝和基本使用。下面的範例把 Univer Docs 掛載到 Astro 頁面中的一個容器裡,該容器設定了明確的高度。

初始化程式碼寫在普通的 <script> 標籤中,以便在瀏覽器中執行。首次載入時以及 astro:page-load 事件觸發時都要執行一次初始化;使用 ClientRouter 時,在 astro:before-swap 之前銷燬編輯器。編輯器相關的 import 語句不要寫入 Astro frontmatter。導航事件的說明見 Astro 導航事件。

整合步驟

  1. 在 mount() / astro:page-load 中初始化 Univer。
  2. 在 astro:before-swap 中銷燬它。

預設設定

UniverDocsCorePreset

  • container
    string | HTMLElement預設值: 'app'
    掛載 Univer 的元素,或該元素的 DOM ID。

範例程式碼

Astro
<div id="univer-docs" style="height: 600px"></div><script>  import type { Univer } from '@univerjs/presets'  import { UniverDocsCorePreset } from '@univerjs/preset-docs-core'  import UniverPresetDocsCorezhTW from '@univerjs/preset-docs-core/locales/zh-TW'  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_TW,      locales: { [LocaleType.ZH_TW]: mergeLocales(UniverPresetDocsCorezhTW) },      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.