在 Astro 中整合
本頁從安裝和基本使用開始。範例將 Univer Bases 掛載到 Astro 頁面中一個具有明確高度的容器內。
使用普通的 <script> 直接在瀏覽器中初始化編輯器。首次載入時執行初始化,並在 astro:page-load 時再次執行;使用 ClientRouter 時,需在 astro:before-swap 之前銷燬例項。編輯器相關的匯入不要放入 Astro frontmatter。詳見 Astro 導航事件。
整合步驟
- 在
mount()/astro:page-load中初始化 Univer。 - 在
astro:before-swap中銷燬該例項。
外掛設定
UniverUIPlugin
containerstring | HTMLElement掛載目標的 DOM 元素或其 ID。
UniverLicensePlugin
licensestring用戶端授權檔的內容。
範例程式碼
Astro
<div id="univer-bases" style="height: 600px"></div><script> import { UniverBasesPlugin } from '@univerjs-pro/bases' import { UniverBasesUIPlugin } from '@univerjs-pro/bases-ui' import BasesUIzhTW from '@univerjs-pro/bases-ui/locale/zh-TW' import BaseszhTW from '@univerjs-pro/bases/locale/zh-TW' import { UniverProFormulaEnginePlugin } from '@univerjs-pro/engine-formula' import { UniverLicensePlugin } from '@univerjs-pro/license' import { LocaleType, mergeLocales, Univer, UniverInstanceType } from '@univerjs/core' import DesignzhTW from '@univerjs/design/locale/zh-TW' import { UniverRenderEnginePlugin } from '@univerjs/engine-render' import { UniverUIPlugin } from '@univerjs/ui' import UIzhTW from '@univerjs/ui/locale/zh-TW' 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_TW, locales: { [LocaleType.ZH_TW]: mergeLocales( DesignzhTW, UIzhTW, BaseszhTW, BasesUIzhTW, ), }, }) 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>你覺得這篇文檔如何?