vue

vue

熱門

Vue 3 Composition API、script setup 巨集、響應式系統及內建元件。適用於撰寫 Vue SFC、使用 defineProps/defineEmits/defineModel、監聽器,或使用 Transition/Teleport/Suspense/KeepAlive 時。

5356星標
304分支
更新於 2026/6/22
SKILL.md
readonlyread-only
name
vue
description

Vue 3 Composition API、script setup 巨集、響應式系統及內建元件。適用於撰寫 Vue SFC、使用 defineProps/defineEmits/defineModel、監聽器,或使用 Transition/Teleport/Suspense/KeepAlive 時。

Vue

基於 Vue 3.5。一律使用 Composition API 搭配 <script setup lang="ts">

偏好設定

  • 偏好 TypeScript 而非 JavaScript
  • 偏好 <script setup lang="ts"> 而非 <script>
  • 為效能考量,若不需要深層響應,優先使用 shallowRef 而非 ref
  • 一律使用 Composition API 而非 Options API
  • 不鼓勵使用響應式 Props 解構

核心

主題 說明 參考資料
Script Setup 與巨集 <script setup>、defineProps、defineEmits、defineModel、defineExpose、defineOptions、defineSlots、泛型 script-setup-macros
響應式與生命週期 ref、shallowRef、computed、watch、watchEffect、effectScope、生命週期鉤子、composables core-new-apis

功能

主題 說明 參考資料
內建元件與指令 Transition、Teleport、Suspense、KeepAlive、v-memo、自訂指令 advanced-patterns

快速參考

元件模板

<script setup lang="ts">
import { ref, computed, watch, onMounted } from 'vue'

const props = defineProps<{
  title: string
  count?: number
}>()

const emit = defineEmits<{
  update: [value: string]
}>()

const model = defineModel<string>()

const doubled = computed(() => (props.count ?? 0) * 2)

watch(() => props.title, (newVal) => {
  console.log('標題變更:', newVal)
})

onMounted(() => {
  console.log('元件已掛載')
})
</script>

<template>
  <div>{{ title }} - {{ doubled }}</div>
</template>

關鍵匯入

// 響應式
import { ref, shallowRef, computed, reactive, readonly, toRef, toRefs, toValue } from 'vue'

// 監聽器
import { watch, watchEffect, watchPostEffect, onWatcherCleanup } from 'vue'

// 生命週期
import { onMounted, onUpdated, onUnmounted, onBeforeMount, onBeforeUpdate, onBeforeUnmount } from 'vue'

// 工具
import { nextTick, defineComponent, defineAsyncComponent } from 'vue'