# 在 Vue 中整合

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

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

- Requested language: `zh-TW`

- Content language: `zh-TW`

- Documentation version: `1.2.0`

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

---

> [!NOTE]
> 如果你正在尋找使用 Vue 元件作為自訂元件的方法，請參閱[此處](https://docs.univer.ai/zh-TW/guides/docs/ui/components.md#vue)。

將 Univer 整合到 Vue 專案前，請先閱讀[安裝與基本使用](https://docs.univer.ai/zh-TW/guides/docs/getting-started/installation.md)章節，並了解 Vue 元件與生命週期鉤子。

## Vue 3.x

### 整合前須知

* 應避免對 Univer 和 FUniver 的例項進行代理，否則可能會引起不可預知的錯誤或效能問題。

### 整合步驟

1. 在 `onMounted` 鉤子中初始化 Univer
2. 在 `onBeforeUnmount` 鉤子中銷燬 Univer

### 示例程式碼

```vue
<script lang="ts" setup>
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 { onBeforeUnmount, onMounted, ref } from 'vue'

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

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

let univerInstance: Univer | null = null

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

  univerAPI.createDocument({})

  univerInstance = univer
})

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

<template>
  <div ref="container" style="height: 600px" />
</template>
```

## Vue 2.x

### 整合前須知

* 使用 pnpm 安裝 `@univerjs/presets` 時，可能會出現需要手動安裝 react 和 react-dom 的情況。
* 如果在開發環境中遇到了 ResizeObserver loop limit exceeded 警告，可以忽略它，也可以參考下面的程式碼片段解決：

```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)
    }
  }
}
```

### 整合步驟

1. 在 `mounted` 鉤子中初始化 Univer
2. 在 `beforeDestroy` 鉤子中銷燬 Univer

### 示例程式碼

```vue
<script>
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'

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

    univerAPI.createDocument({})

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

<template>
  <div ref="container" style="height: 600px" />
</template>
```
