Integrate in Vue
To use Vue components as custom components in the editor, see custom components.
Before integrating Univer into a Vue project, read installation and basic usage and become familiar with Vue components and lifecycle hooks.
Vue 3.x
Notes before integration
- Do not proxy the Univer or FUniver instances. Proxies can cause unpredictable errors or performance problems.
Integration steps
- Initialize Univer in
onMounted. - Dispose of Univer in
onBeforeUnmount.
Preset configuration
UniverSheetsCorePreset
containerstring | HTMLElementDefault:'app'The element into which Univer is mounted, or the DOM ID of that element.
Example
<script lang="ts" setup>import type { Univer } from '@univerjs/presets'import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import { onBeforeUnmount, onMounted, ref } from 'vue'import '@univerjs/preset-sheets-core/lib/index.css'const container = ref<HTMLElement | null>(null)let univerInstance: Univer | null = nullonMounted(() => { const { univer, univerAPI } = createUniver({ locale: LocaleType.EN_US, locales: { [LocaleType.EN_US]: mergeLocales( UniverPresetSheetsCoreEnUS, ), }, presets: [ UniverSheetsCorePreset({ container: container.value as HTMLElement, }), ], }) univerAPI.createWorkbook({}) univerInstance = univer})onBeforeUnmount(() => { univerInstance?.dispose() univerInstance = null})</script><template> <div ref="container" style="height: 600px" /></template>Vue 2.x
This section applies to existing Vue 2 applications. The example uses Vue 2.7.16, Vite 6.3.6, and @vitejs/plugin-vue2 2.3.3. Start new projects with Vue 3.
To try the example from an empty project, download and extract the starter, run npm install and npm run dev, and open the URL printed in the terminal. The starter interface is in English. If you already have a Vue 2 project, follow the steps below.
Install matching dependencies
npm install @univerjs/presets @univerjs/preset-sheets-core react@18.3.1 react-dom@18.3.1 rxjs@7.8.2Add this component to your page
Keep the Univer instance and Facade objects out of reactive data. Create the editor in mounted, once the container exists, and call univer.dispose() in beforeDestroy. Do not dispose of the same instance again through Facade.
<script>import { UniverSheetsCorePreset } from '@univerjs/preset-sheets-core'import UniverPresetSheetsCoreEnUS from '@univerjs/preset-sheets-core/locales/en-US'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import '@univerjs/preset-sheets-core/lib/index.css'export default { mounted() { const { univer, univerAPI } = createUniver({ locale: LocaleType.EN_US, locales: { [LocaleType.EN_US]: mergeLocales( UniverPresetSheetsCoreEnUS, ), }, presets: [ UniverSheetsCorePreset({ container: this.$refs.container, }), ], }) this.univerInstance = univer this.univerAPI = univerAPI const workbook = univerAPI.createWorkbook({}) workbook.getActiveSheet().getRange('A1:D3').setValues([ ['Item', 'Quantity', 'Unit price', 'Total'], ['Notebook', 3, 12, '=B2*C2'], ['Pen', 5, 2, '=B3*C3'], ]) }, beforeDestroy() { this.univerInstance?.dispose() this.univerInstance = null this.univerAPI = null },}</script><template> <div ref="container" style="height: 600px" /></template>Check the result
The total in the first row is 36. Change the quantity from 3 to 4, and the total becomes 48. Then remove and remount the component to verify that cleanup works.
If initialization fails
If a setConfig error occurs, compare your setup with the starter and inspect the dependency tree with the command below. Keep one matching set of Univer packages, plus a compatible React/ReactDOM pair. A stack trace alone does not prove that Vue 2 is unsupported.
npm ls @univerjs/core @univerjs/ui @univerjs/presets @univerjs/preset-sheets-core react react-domIf only the editor area is blank, check that its container has a height and that the preset CSS is imported. Do not make the Univer instance reactive solely to keep it on the component.
Webpack 4 / Vue CLI 3–4
The starter uses Vite and package exports, so a successful build there does not verify Webpack 4. If your legacy build cannot resolve Facade or locale exports, upgrade the build tool. Copying part of an alias list can hide missing or duplicate modules.
How is this guide?