vue
antfu/skills
使用帶有 TypeScript 的組合式 API 編寫 Vue 3 單檔案元件,涵蓋 script setup 宏、響應式系統、內建元件以及生命週期鉤子。
...展開全部Vue
基於 Vue 3.5。始終使用帶有 `` 的組合式 API。
偏好設定
- 優先使用 TypeScript 而非 JavaScript
- 優先使用
而非 - 出於效能考慮,如果不需要深層響應性,優先使用
shallowRef而非ref - 始終使用組合式 API 而非選項式 API
- 不鼓勵使用響應式 Props 解構
核心
| 主題 | 描述 | 參考 |
|---|---|---|
| 指令碼設定與宏 | ``, defineProps, defineEmits, defineModel, defineExpose, defineOptions, defineSlots, 泛型 | script-setup-macros |
| 響應性與生命週期 | ref, shallowRef, computed, watch, watchEffect, effectScope, 生命週期鉤子, 組合式函式 | core-new-apis |
功能
| 主題 | 描述 | 參考 |
|---|---|---|
| 內建元件與指令 | Transition, Teleport, Suspense, KeepAlive, v-memo, 自定義指令 | advanced-patterns |
快速參考
元件模板
import { ref, computed, watch, onMounted } from 'vue'
const props = defineProps()
const emit = defineEmits()
const model = defineModel<string>()
const doubled = computed(() => (props.count ?? 0) * 2)
watch(() => props.title, (newVal) => {
console.log('標題已更改:', newVal)
})
onMounted(() => {
console.log('元件已掛載')
})
<template>
{{ title }} - {{ doubled }}
</template></string>關鍵匯入
// 響應性
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'
---
name: vue
description: Write Vue 3 Single-File Components using Composition API with TypeScript, covering script setup macros, reactivity system, built-in components, and lifecycle hooks.
---
# Vue
> Based on Vue 3.5. Always use Composition API with `<script setup lang="ts">`.
## Preferences
- Prefer TypeScript over JavaScript
- Prefer `<script setup lang="ts">` over `<script>`
- For performance, prefer `shallowRef` over `ref` if deep reactivity is not needed
- Always use Composition API over Options API
- Discourage using Reactive Props Destructure
## Core
| Topic | Description | Reference |
|-------|-------------|-----------|
| Script Setup & Macros | `<script setup>`, defineProps, defineEmits, defineModel, defineExpose, defineOptions, defineSlots, generics | [script-setup-macros](references/script-setup-macros.md) |
| Reactivity & Lifecycle | ref, shallowRef, computed, watch, watchEffect, effectScope, lifecycle hooks, composables | [core-new-apis](references/core-new-apis.md) |
## Features
| Topic | Description | Reference |
|-------|-------------|-----------|
| Built-in Components & Directives | Transition, Teleport, Suspense, KeepAlive, v-memo, custom directives | [advanced-patterns](references/advanced-patterns.md) |
## Quick Reference
### Component Template
```vue
<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('Title changed:', newVal)
})
onMounted(() => {
console.log('Component mounted')
})
</script>
<template>
<div>{{ title }} - {{ doubled }}</div>
</template>
```
### Key Imports
```ts
// Reactivity
import { ref, shallowRef, computed, reactive, readonly, toRef, toRefs, toValue } from 'vue'
// Watchers
import { watch, watchEffect, watchPostEffect, onWatcherCleanup } from 'vue'
// Lifecycle
import { onMounted, onUpdated, onUnmounted, onBeforeMount, onBeforeUpdate, onBeforeUnmount } from 'vue'
// Utilities
import { nextTick, defineComponent, defineAsyncComponent } from 'vue'
```
所有檔案
0 個檔案安裝 vue
將技能檔案下載並解壓至你的 .claude/skills/ 目錄。
下載 ZIP複製儲存庫並將技能檔案複製到您的專案中。
git clone https://github.com/antfu/skills/tree/main/skills/vue # Copy SKILL.md to your .claude/skills/ directory
複製





首頁
