# Vue

- Human documentation: [https://docs.univer.ai/guides/docs/getting-started/integrations/vue](https://docs.univer.ai/guides/docs/getting-started/integrations/vue)

- Agent Markdown: [https://docs.univer.ai/guides/docs/getting-started/integrations/vue.md](https://docs.univer.ai/guides/docs/getting-started/integrations/vue.md)

- Requested language: `en-US`

- Content language: `en-US`

- Documentation version: `1.0.0-rc.0`

- Source: [docs/getting-started/integrations/vue.mdx](https://github.com/dream-num/documentation/blob/dev/content/guides/docs/getting-started/integrations/vue.mdx)

---

> [!NOTE]
> If you are looking for how to use Vue components as custom components, please refer to [here](https://docs.univer.ai/guides/docs/ui/components.md#vue).

## Vue 3.x

### Notes before integration

* Avoid proxying the Univer and FUniver instances, as this may lead to unpredictable errors or performance issues.

### Integration Steps

1. Initialize Univer in the `onMounted` hook
2. Destroy Univer in the `onBeforeUnmount` hook

### Example

```vue
<script lang="ts" setup>
import type { FUniver, Univer } from '@univerjs/presets'
import { UniverDocsCorePreset } from '@univerjs/preset-docs-core'
import UniverPresetDocsCoreEnUS from '@univerjs/preset-docs-core/locales/en-US'
import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'
import { onBeforeUnmount, onMounted, ref } from 'vue'

import '@univerjs/preset-docs-core/lib/index.css'

const container = ref<HTMLElement | null>(null)

let univerInstance: Univer | null = null
let univerAPIInstance: FUniver | null = null

onMounted(() => {
  const { univer, univerAPI } = createUniver({
    locale: LocaleType.EN_US,
    locales: {
      [LocaleType.EN_US]: mergeLocales(
        UniverPresetDocsCoreEnUS,
      ),
    },
    presets: [
      UniverDocsCorePreset({
        container: container.value as HTMLElement,
      }),
    ],
  })

  univerAPI.createDocument({})

  univerInstance = univer
  univerAPIInstance = univerAPI
})

onBeforeUnmount(() => {
  univerInstance?.dispose()
  univerAPIInstance?.dispose()
  univerInstance = null
  univerAPIInstance = null
})
</script>

<template>
  <div ref="container" />
</template>
```

## Vue 2.x

### Notes before integration

* When installing `@univerjs/presets` with pnpm, you may need to manually install `react` and `react-dom`.
* If you encounter the "ResizeObserver loop limit exceeded" warning in the development environment, you can ignore it or refer to the code snippet below to resolve it:

```typescript
if (process.env.NODE_ENV === 'development') {
  const _ResizeObserver = window.ResizeObserver
  function debounce(fn, delay) {
    let timer = null
    return function () {
      if (timer) {
        clearTimeout(timer)
      }
      timer = setTimeout(() => {
        fn.apply(this, arguments)
      }, delay)
    }
  }
  window.ResizeObserver = class ResizeObserver extends _ResizeObserver {
    constructor(callback) {
      callback = debounce(callback, 50)
      super(callback)
    }
  }
}
```

### Integration Steps

1. Initialize Univer in the `mounted` hook
2. Destroy Univer in the `beforeDestroy` hook

### Example

```vue
<script>
import { UniverDocsCorePreset } from '@univerjs/preset-docs-core'
import UniverPresetDocsCoreEnUS from '@univerjs/preset-docs-core/locales/en-US'
import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'

import '@univerjs/preset-docs-core/lib/index.css'

export default {
  mounted() {
    const { univer, univerAPI } = createUniver({
      locale: LocaleType.EN_US,
      locales: {
        [LocaleType.EN_US]: mergeLocales(
          UniverPresetDocsCoreEnUS,
        ),
      },
      presets: [
        UniverDocsCorePreset({
          container: this.$refs.container,
        }),
      ],
    })

    univerAPI.createDocument({})

    this.univerInstance = univer
    this.univerAPIInstance = univerAPI
  },
  beforeUnmount() {
    this.univerInstance?.dispose()
    this.univerAPIInstance?.dispose()
    this.univerInstance = null
    this.univerAPIInstance = null
  },
}
</script>

<template>
  <div ref="container" />
</template>
```
