vue
Vue
基于 Vue 3.5。始终使用
<script setup lang="ts">。
偏好
- 优先使用 TypeScript 而非 JavaScript
- 优先使用
<script setup lang="ts">而非<script> - 为性能考虑,如不需要深层响应式,优先使用
shallowRef而非ref - 始终使用组合式 API 而非选项式 API
- 不鼓励使用响应式 Props 解构
核心
| 主题 | 描述 | 参考 |
|---|---|---|
| Script Setup 和宏 | <script setup>、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 |
快速参考
组件模板
<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>
关键导入
// 响应式
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'
More from tnnevol/skills
memos
Assistant for Memos (https://github.com/usememos/memos), an open-source self-hosted memo/note tool. Use when the user asks about Memos, creating/listing/updating/deleting memos, managing tags, or securely interacting with a Memos instance via API.
13pnpm
Node.js 包管理器,具有严格的依赖解析。在运行 pnpm 特定命令、配置工作区或使用目录、补丁或覆盖管理依赖项时使用。
8vitepress
VitePress static site generator powered by Vite and Vue. Use when building documentation sites, configuring themes, or writing Markdown with Vue components.
6tsdown
Bundle TypeScript and JavaScript libraries with blazing-fast speed powered by Rolldown. Use when building libraries, generating type declarations, bundling for multiple formats, or migrating from tsup.
6vitest
Vitest fast unit testing framework powered by Vite with Jest-compatible API. Use when writing tests, mocking, configuring coverage, or working with test filtering and fixtures.
6vite
Vite build tool configuration, plugin API, SSR, and Vite 8 Rolldown migration. Use when working with Vite projects, vite.config.ts, Vite plugins, or building libraries/SSR apps with Vite.
6