关于 vue
Vue 3 是用于构建用户界面的渐进式 JavaScript 框架,适合从小型界面到复杂应用的开发。它提供 Composition API、响应式系统和单文件组件,并涵盖路由、状态管理与 TypeScript 用法。
主要功能
- 01Composition API 与响应式状态
- 02单文件组件组织模板、脚本和样式
- 03Vue Router 路由与导航守卫
- 04Pinia 状态管理
- 05TypeScript 组件类型支持
- 06组合式函数复用逻辑
适用场景
- 01构建 Vue 3 用户界面和单页应用
- 02在组件间复用业务逻辑
- 03为应用组织路由与状态管理
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ bobmatnyc/ claude-mpm-skills --skill vue安装后在 Claude Code 中输入/vue确认已加载。
附带文件metadata.json
详细介绍
Vue 3 - 渐进式 JavaScript 框架
概述
Vue 3 是一个渐进式框架,用于构建用户界面,重点关注易用性、性能和灵活性。它提供了便于复用逻辑的 Composition API、强大的响应式系统,以及单文件组件(.vue 文件)。
主要特性:
- Composition API:使用 ref、reactive、computed、watch 的 setup()
- 响应式系统:细粒度的响应式数据跟踪
- 单文件组件:在一个文件中编写模板、脚本和样式
- Vue Router:为 SPA 提供的官方路由
- Pinia:现代状态管理(Vuex 的继任者)
- TypeScript:一流的 TypeScript 支持
- Vite:支持 HMR 的极速开发体验
安装:
# Create new Vue 3 project (recommended)
npm create vue@latest my-app
cd my-app
npm install
npm run dev
# Or with Vite template
npm create vite@latest my-app -- --template vue-ts
Composition API 基础
setup() 函数
<script setup lang="ts">
// Modern <script setup> syntax (recommended)
import { ref, computed, onMounted } from 'vue';
// Reactive state
const count = ref(0);
const message = ref('Hello Vue 3');
// Computed values
const doubled = computed(() => count.value * 2);
// Methods
function increment() {
count.value++;
}
// Lifecycle hooks
onMounted(() => {
console.log('Component mounted');
});
</script>
<template>
<div>
<p>Count: {{ count }} (Doubled: {{ doubled }})</p>
<button @click="increment">Increment</button>
</div>
</template>
使用 ref() 和 reactive() 管理响应式状态
<script setup lang="ts">
import { ref, reactive } from 'vue';
// ref() - for primitives and objects (needs .value in script)
const count = ref(0);
const user = ref({ name: 'Alice', age: 30 });
console.log(count.value); // 0
console.log(user.value.name); // 'Alice'
// reactive() - for objects only (no .value needed)
const state = reactive({
todos: [] as Todo[],
filter: 'all',
error: null as string | null
});
console.log(state.todos); // []
state.todos.push({ id: 1, text: 'Learn Vue', done: false });
</script>
<template>
<p>Count: {{ count }}</p>
<p>User: {{ user.name }}</p>
<p>Todos: {{ state.todos.length }}</p>
</template>
计算属性
<script setup lang="ts">
import { ref, computed } from 'vue';
const firstName = ref('John');
const lastName = ref('Doe');
// Read-only computed
const fullName = computed(() => `${firstName.value} ${lastName.value}`);
// Writable computed
const fullNameWritable = computed({
get() {
return `${firstName.value} ${lastName.value}`;
},
set(value: string) {
const parts = value.split(' ');
firstName.value = parts[0];
lastName.value = parts[1];
}
});
// Complex computations
interface Todo {
id: number;
text: string;
done: boolean;
}
const todos = ref<Todo[]>([
{ id: 1, text: 'Learn Vue', done: true },
{ id: 2, text: 'Build app', done: false }
]);
const completedTodos = computed(() =>
todos.value.filter(t => t.done)
);
const activeTodos = computed(() =>
todos.value.filter(t => !t.done)
);
const progress = computed(() =>
todos.value.length > 0
? (completedTodos.value.length / todos.value.length) * 100
: 0
);
</script>
<template>
<div>
<p>Full Name: {{ fullName }}</p>
<p>Progress: {{ progress.toFixed(1) }}%</p>
<p>Active: {{ activeTodos.length }} | Done: {{ completedTodos.length }}</p>
</div>
</template>
侦听器和副作用
<script setup lang="ts">
import { ref, watch, watchEffect } from 'vue';
const count = ref(0);
const user = ref({ name: 'Alice', age: 30 });
// watch() - explicit dependencies
watch(count, (newVal, oldVal) => {
console.log(`Count changed from ${oldVal} to ${newVal}`);
});
// Watch multiple sources
watch([count, user], ([newCount, newUser], [oldCount, oldUser]) => {
console.log('Count or user changed');
});
// Watch object property (needs getter)
watch(
() => user.value.name,
(newName, oldName) => {
console.log(`Name changed from ${oldName} to ${newName}`);
}
);
// Deep watch for nested objects
watch(
user,
(newUser) => {
console.log('User object changed deeply');
},
{ deep: true }
);
// watchEffect() - automatic dependency tracking
watchEffect(() => {
// Automatically watches count and user
console.log(`Count: ${count.value}, User: ${user.value.name}`);
});
// Cleanup function
watchEffect((onCleanup) => {
const timer = setTimeout(() => {
console.log('Delayed effect');
}, 1000);
onCleanup(() => {
clearTimeout(timer);
});
});
</script>
组件 Props 和事件
定义 Props(TypeScript)
<script setup lang="ts">
// Type-safe props with defineProps
interface Props {
title: string;
count?: number;
tags?: string[];
user: {
name: string;
email: string;
};
disabled?: boolean;
}
// With defaults
const props = withDefaults(defineProps<Props>(), {
count: 0,
tags: () => [],
disabled: false
});
// Access props
console.log(props.title);
console.log(props.count);
</script>
<template>
<div>
<h1>{{ title }}</h1>
<p>Count: {{ count }}</p>
<p>Tags: {{ tags.join(', ') }}</p>
</div>
</template>
触发事件
<script setup lang="ts">
// Define emitted events with types
const emit = defineEmits<{
update: [value: number];
submit: [data: { name: string; email: string }];
delete: [id: number];
}>();
function handleClick() {
emit('update', 42);
}
function handleSubmit() {
emit('submit', { name: 'Alice', email: '[email protected]' });
}
</script>
<template>
<button @click="handleClick">Update</button>
<button @click="handleSubmit">Submit</button>
</template>
用于双向绑定的 v-model
<script setup lang="ts">
// v-model creates 'modelValue' prop and 'update:modelValue' event
const props = defineProps<{
modelValue: string;
placeholder?: string;
}>();
const emit = defineEmits<{
'update:modelValue': [value: string];
}>();
function handleInput(event: Event) {
const target = event.target as HTMLInputElement;
emit('update:modelValue', target.value);
}
</script>
<template>
<input
:value="modelValue"
@input="handleInput"
:placeholder="placeholder"
/>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import CustomInput from './CustomInput.vue';
const searchQuery = ref('');
</script>
<template>
<CustomInput v-model="searchQuery" placeholder="Search..." />
<p>Searching for: {{ searchQuery }}</p>
</template>
多个 v-model 绑定
<script setup lang="ts">
defineProps<{
firstName: string;
lastName: string;
}>();
const emit = defineEmits<{
'update:firstName': [value: string];
'update:lastName': [value: string];
}>();
</script>
<template>
<div>
<input
:value="firstName"
@input="emit('update:firstName', ($event.target as HTMLInputElement).value)"
/>
<input
:value="lastName"
@input="emit('update:lastName', ($event.target as HTMLInputElement).value)"
/>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import UserForm from './UserForm.vue';
const first = ref('John');
const last = ref('Doe');
</script>
<template>
<UserForm v-model:first-name="first" v-model:last-name="last" />
<p>Full name: {{ first }} {{ last }}</p>
</template>
模板语法
指令
<script setup lang="ts">
import { ref, reactive } from 'vue';
const message = ref('Hello Vue');
const isActive = ref(true);
const hasError = ref(false);
const items = ref(['Apple', 'Banana', 'Cherry']);
const user = ref({ name: 'Alice', email: '[email protected]' });
const formData = reactive({
username: '',
agree: false,
gender: 'male',
interests: [] as string[]
});
</script>
<template>
<p>{{ message }}</p>
<div v-html="'<strong>Bold</strong>'"></div>
<div :id="'container-' + user.name"></div>
<img :src="user.avatar" :alt="user.name" />
<div :class="{ active: isActive, 'text-danger': hasError }"></div>
<div :class="[isActive ? 'active' : '', hasError && 'error']"></div>
<div :style="{ color: 'red', fontSize: '16px' }"></div>
<div :style="{ color: isActive ? 'green' : 'gray' }"></div>
<p v-if="isActive">Active</p>
<p v-else-if="hasError">Error</p>
<p v-else>Inactive</p>
<p v-show="isActive">Visible when active</p>
<ul>
<li v-for="(item, index) in items" :key="index">
{{ index + 1 }}. {{ item }}
</li>
</ul>
<div v-for="(value, key) in user" :key="key">
{{ key }}: {{ value }}
</div>
<button @click="isActive = !isActive">Toggle</button>
<button @click.prevent="handleSubmit">Submit</button>
<input @keyup.enter="handleSearch" />
<input v-model="formData.username" />
<input type="checkbox" v-model="formData.agree" />
<input type="radio" v-model="formData.gender" value="male" />
<input type="radio" v-model="formData.gender" value="female" />
<select v-model="formData.interests" multiple>
<option>Reading</option>
<option>Gaming</option>
<option>Coding</option>
</select>
</template>
事件修饰符
<template>
<form @submit.prevent="handleSubmit">
<button type="submit">Submit</button>
</form>
<div @click="handleOuter">
<button @click.stop="handleInner">Click me</button>
</div>
<div @click.capture="handleCapture">...</div>
<div @click.self="handleSelf">...</div>
<button @click.once="handleOnce">Click once</button>
<input @keyup.enter="handleEnter" />
<input @keyup.esc="handleEscape" />
<input @keyup.ctrl.s="handleSave" />
<input @keyup.shift.t="handleShiftT" />
<div @click.left="handleLeftClick"></div>
<div @click.right="handleRightClick"></div>
<div @click.middle="handleMiddleClick"></div>
</template>
生命周期钩子
<script setup lang="ts">
import {
onBeforeMount,
onMounted,
onBeforeUpdate,
onUpdated,
onBeforeUnmount,
onUnmounted,
onErrorCaptured
} from 'vue';
// Before component is mounted
onBeforeMount(() => {
console.log('Component about to mount');
});
// After component is mounted (DOM is ready)
onMounted(() => {
console.log('Component mounted');
// Good place for API calls, DOM manipulation
fetchData();
});
// Before component updates due to reactive changes
onBeforeUpdate(() => {
console.log('Component about to update');
});
// After component updates
onUpdated(() => {
console.log('Component updated');
// Careful: can cause infinite loops if you update state here
});
// Before component unmounts
onBeforeUnmount(() => {
console.log('Component about to unmount');
// Clean up subscriptions, timers, etc.
});
// After component unmounts
onUnmounted(() => {
console.log('Component unmounted');
});
// Error handling
onErrorCaptured((err, instance, info) => {
console.error('Error captured:', err, info);
return false; // Prevent propagation
});
async function fetchData() {
const response = await fetch('/api/data');
const data = await response.json();
console.log(data);
}
</script>
Provide/Inject(依赖注入)
<script setup lang="ts">
import { ref, provide } from 'vue';
import type { InjectionKey } from 'vue';
interface Theme {
primary: string;
secondary: string;
}
// Create typed injection key
export const ThemeKey: InjectionKey<Theme> = Symbol('theme');
const theme = ref<Theme>({
primary: '#007bff',
secondary: '#6c757d'
});
// Provide to all descendants
provide(ThemeKey, theme.value);
provide('userPermissions', ['read', 'write']);
</script>
<script setup lang="ts">
import { inject } from 'vue';
import { ThemeKey } from './Parent.vue';
// Inject with type safety
const theme = inject(ThemeKey);
const permissions = inject<string[]>('userPermissions', []);
// With default value
const config = inject('config', { debug: false });
</script>
<template>
<div :style="{ color: theme?.primary }">
Themed content
</div>
</template>
集成 Vue Router
基本设置
// router/index.ts
import { createRouter, createWebHistory } from 'vue-router';
import type { RouteRecordRaw } from 'vue-router';
import Home from '@/views/Home.vue';
import About from '@/views/About.vue';
const routes: RouteRecordRaw[] = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/about',
name: 'About',
component: About
},
{
path: '/user/:id',
name: 'User',
component: () => import('@/views/User.vue'), // Lazy loading
props: true // Pass route params as props
},
{
path: '/dashboard',
name: 'Dashboard',
component: () => import('@/views/Dashboard.vue'),
meta: { requiresAuth: true }
},
{
path: '/:pathMatch(.*)*',
name: 'NotFound',
component: () => import('@/views/NotFound.vue')
}
];
const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
routes
});
export default router;
导航和路由访问
<script setup lang="ts">
import { useRouter, useRoute } from 'vue-router';
import { computed } from 'vue';
const router = useRouter();
const route = useRoute();
// Access route params
const userId = computed(() => route.params.id);
const querySearch = computed(() => route.query.search);
// Programmatic navigation
function goToUser(id: number) {
router.push({ name: 'User', params: { id } });
}
function goToAbout() {
router.push('/about');
}
function goBack() {
router.back();
}
function replaceRoute() {
router.replace({ name: 'Home' }); // No history entry
}
</script>
<template>
<nav>
<RouterLink to="/">Home</RouterLink>
<RouterLink :to="{ name: 'About' }">About</RouterLink>
<RouterLink :to="{ name: 'User', params: { id: 123 } }">
User 123
</RouterLink>
<RouterLink
to="/dashboard"
active-class="active"
exact-active-class="exact-active"
>
Dashboard
</RouterLink>
</nav>
<button @click="goToUser(456)">Go to User 456</button>
<button @click="goBack">Back</button>
<p>Current user ID: {{ userId }}</p>
<p>Search query: {{ querySearch }}</p>
<RouterView />
</template>
导航守卫
// router/index.ts
import { createRouter } from 'vue-router';
const router = createRouter({
// ... routes
});
// Global before guard
router.beforeEach((to, from, next) => {
const isAuthenticated = checkAuth();
if (to.meta.requiresAuth && !isAuthenticated) {
next({ name: 'Login', query: { redirect: to.fullPath } });
} else {
next();
}
});
// Global after hook
router.afterEach((to, from) => {
document.title = `${to.meta.title || 'App'} - My App`;
});
// Per-route guard
const routes = [
{
path: '/admin',
component: Admin,
beforeEnter: (to, from, next) => {
if (isAdmin()) {
next();
} else {
next('/unauthorized');
}
}
}
];
<script setup lang="ts">
import { onBeforeRouteLeave, onBeforeRouteUpdate } from 'vue-router';
// Confirm before leaving
onBeforeRouteLeave((to, from) => {
if (hasUnsavedChanges.value) {
const answer = window.confirm('You have unsaved changes. Leave anyway?');
return answer;
}
});
// React to route changes (same component, different params)
onBeforeRouteUpdate((to, from) => {
console.log(`Route updated from ${from.params.id} to ${to.params.id}`);
fetchData(to.params.id);
});
</script>
Pinia 状态管理
Store 定义
// stores/counter.ts
import { defineStore } from 'pinia';
import { ref, computed } from 'vue';
// Composition API style (recommended)
export const useCounterStore = defineStore('counter', () => {
// State
const count = ref(0);
const name = ref('Counter Store');
// Getters (computed)
const doubleCount = computed(() => count.value * 2);
const isPositive = computed(() => count.value > 0);
// Actions
function increment() {
count.value++;
}
function decrement() {
count.value--;
}
async function fetchCount() {
const response = await fetch('/api/count');
const data = await response.json();
count.value = data.count;
}
return {
count,
name,
doubleCount,
isPositive,
increment,
decrement,
fetchCount
};
});
// Options API style (alternative)
export const useUserStore = defineStore('user', {
state: () => ({
user: null as User | null,
token: ''
}),
getters: {
isLoggedIn: (state) => state.user !== null,
fullName: (state) => state.user ? `${state.user.firstName} ${state.user.lastName}` : ''
},
actions: {
async login(email: string, password: string) {
const response = await fetch('/api/login', {
method: 'POST',
body: JSON.stringify({ email, password })
});
const data = await response.json();
this.user = data.user;
this.token = data.token;
},
logout() {
this.user = null;
this.token = '';
}
}
});
在组件中使用 Store
<script setup lang="ts">
import { useCounterStore } from '@/stores/counter';
import { useUserStore } from '@/stores/user';
import { storeToRefs } from 'pinia';
const counterStore = useCounterStore();
const userStore = useUserStore();
// Get reactive refs from store
const { count, doubleCount } = storeToRefs(counterStore);
const { user, isLoggedIn } = storeToRefs(userStore);
// Actions can be destructured directly (they're not reactive)
const { increment, decrement } = counterStore;
// Access state directly
console.log(counterStore.count);
// Modify state directly
counterStore.count++;
// Or use $patch for multiple changes
counterStore.$patch({
count: 10,
name: 'Updated Counter'
});
// Reset state
counterStore.$reset();
</script>
<template>
<div>
<p>Count: {{ count }} (Double: {{ doubleCount }})</p>
<button @click="increment">+</button>
<button @click="decrement">-</button>
<div v-if="isLoggedIn">
<p>Welcome, {{ user?.firstName }}!</p>
<button @click="userStore.logout()">Logout</button>
</div>
</div>
</template>
Store 组合(访问其他 Store)
// stores/cart.ts
import { defineStore } from 'pinia';
import { ref, computed } from 'vue';
import { useUserStore } from './user';
export const useCartStore = defineStore('cart', () => {
const items = ref<CartItem[]>([]);
const userStore = useUserStore();
const total = computed(() =>
items.value.reduce((sum, item) => sum + item.price * item.quantity, 0)
);
const canCheckout = computed(() =>
userStore.isLoggedIn && items.value.length > 0
);
async function checkout() {
if (!canCheckout.value) return;
await fetch('/api/checkout', {
method: 'POST',
headers: {
Authorization: `Bearer ${userStore.token}`
},
body: JSON.stringify({ items: items.value })
});
items.value = [];
}
return { items, total, canCheckout, checkout };
});
Composables(可复用逻辑)
自定义 Composables
// composables/useFetch.ts
import { ref, type Ref } from 'vue';
interface UseFetchOptions {
immediate?: boolean;
}
export function useFetch<T>(url: string, options: UseFetchOptions = {}) {
const data = ref<T | null>(null) as Ref<T | null>;
const error = ref<Error | null>(null);
const loading = ref(false);
async function execute() {
loading.value = true;
error.value = null;
try {
const response = await fetch(url);
if (!response.ok) throw new Error(response.statusText);
data.value = await response.json();
} catch (e) {
error.value = e as Error;
} finally {
loading.value = false;
}
}
if (options.immediate) {
execute();
}
return { data, error, loading, execute };
}
// composables/useLocalStorage.ts
import { ref, watch, type Ref } from 'vue';
export function useLocalStorage<T>(key: string, defaultValue: T): Ref<T> {
const storedValue = localStorage.getItem(key);
const data = ref<T>(
storedValue ? JSON.parse(storedValue) : defaultValue
) as Ref<T>;
watch(
data,
(newValue) => {
localStorage.setItem(key, JSON.stringify(newValue));
},
{ deep: true }
);
return data;
}
// composables/useMouse.ts
import { ref, onMounted, onUnmounted } from 'vue';
export function useMouse() {
const x = ref(0);
const y = ref(0);
function update(event: MouseEvent) {
x.value = event.pageX;
y.value = event.pageY;
}
onMounted(() => {
window.addEventListener('mousemove', update);
});
onUnmounted(() => {
window.removeEventListener('mousemove', update);
});
return { x, y };
}
使用 Composables
<script setup lang="ts">
import { useFetch } from '@/composables/useFetch';
import { useLocalStorage } from '@/composables/useLocalStorage';
import { useMouse } from '@/composables/useMouse';
interface User {
id: number;
name: string;
email: string;
}
const { data: user, loading, error, execute } = useFetch<User>(
'/api/user/123',
{ immediate: true }
);
const settings = useLocalStorage('app-settings', {
theme: 'dark',
language: 'en'
});
const { x, y } = useMouse();
</script>
<template>
<div>
<div v-if="loading">Loading...</div>
<div v-else-if="error">Error: {{ error.message }}</div>
<div v-else-if="user">
<h1>{{ user.name }}</h1>
<p>{{ user.email }}</p>
</div>
<p>Theme: {{ settings.theme }}</p>
<button @click="settings.theme = settings.theme === 'dark' ? 'light' : 'dark'">
Toggle Theme
</button>
<p>Mouse: {{ x }}, {{ y }}</p>
</div>
</template>
使用 Vitest 进行测试
组件测试
// Counter.test.ts
import { mount } from '@vue/test-utils';
import { describe, it, expect } from 'vitest';
import Counter from '@/components/Counter.vue';
describe('Counter', () => {
it('renders initial count', () => {
const wrapper = mount(Counter);
expect(wrapper.text()).toContain('Count: 0');
});
it('increments count on button click', async () => {
const wrapper = mount(Counter);
await wrapper.find('button').trigger('click');
expect(wrapper.text()).toContain('Count: 1');
});
it('accepts initial count prop', () => {
const wrapper = mount(Counter, {
props: { initialCount: 10 }
});
expect(wrapper.text()).toContain('Count: 10');
});
it('emits update event', async () => {
const wrapper = mount(Counter);
await wrapper.find('button').trigger('click');
expect(wrapper.emitted('update')).toBeTruthy();
expect(wrapper.emitted('update')![0]).toEqual([1]);
});
});
使用 Pinia 进行测试
// UserProfile.test.ts
import { mount } from '@vue/test-utils';
import { createPinia, setActivePinia } from 'pinia';
import { beforeEach, describe, it, expect } from 'vitest';
import UserProfile from '@/components/UserProfile.vue';
import { useUserStore } from '@/stores/user';
describe('UserProfile', () => {
beforeEach(() => {
setActivePinia(createPinia());
});
it('displays user name when logged in', () => {
const userStore = useUserStore();
userStore.user = { id: 1, firstName: 'Alice', lastName: 'Smith' };
const wrapper = mount(UserProfile);
expect(wrapper.text()).toContain('Alice Smith');
});
it('shows login prompt when not logged in', () => {
const wrapper = mount(UserProfile);
expect(wrapper.text()).toContain('Please log in');
});
});
TypeScript 最佳实践
使用 Interface 定义组件 Props
<script setup lang="ts">
interface User {
id: number;
name: string;
email: string;
role: 'admin' | 'user' | 'guest';
}
interface Props {
user: User;
showEmail?: boolean;
onUpdate?: (user: User) => void;
}
const props = withDefaults(defineProps<Props>(), {
showEmail: true
});
// Type-safe emits
const emit = defineEmits<{
update: [user: User];
delete: [userId: number];
}>();
function handleUpdate() {
emit('update', props.user);
}
</script>
泛型组件
<script setup lang="ts" generic="T">
interface Props<T> {
items: T[];
keyFn: (item: T) => string | number;
renderItem: (item: T) => string;
}
const props = defineProps<Props<T>>();
</script>
<template>
<ul>
<li v-for="item in items" :key="keyFn(item)">
{{ renderItem(item) }}
</li>
</ul>
</template>
性能优化
为大型列表使用虚拟滚动
<script setup lang="ts">
import { ref, computed } from 'vue';
const items = ref(Array.from({ length: 10000 }, (_, i) => ({
id: i,
text: `Item ${i}`
})));
const containerHeight = 400;
const itemHeight = 40;
const scrollTop = ref(0);
const visibleCount = Math.ceil(containerHeight / itemHeight);
const startIndex = computed(() => Math.floor(scrollTop.value / itemHeight));
const endIndex = computed(() => startIndex.value + visibleCount);
const visibleItems = computed(() =>
items.value.slice(startIndex.value, endIndex.value)
);
const offsetY = computed(() => startIndex.value * itemHeight);
const totalHeight = computed(() => items.value.length * itemHeight);
function handleScroll(event: Event) {
scrollTop.value = (event.target as HTMLElement).scrollTop;
}
</script>
<template>
<div
class="virtual-list"
:style="{ height: containerHeight + 'px', overflow: 'auto' }"
@scroll="handleScroll"
>
<div :style="{ height: totalHeight + 'px', position: 'relative' }">
<div :style="{ transform: `translateY(${offsetY}px)` }">
<div
v-for="item in visibleItems"
:key="item.id"
:style="{ height: itemHeight + 'px' }"
>
{{ item.text }}
</div>
</div>
</div>
</div>
</template>
延迟加载组件
<script setup lang="ts">
import { defineAsyncComponent } from 'vue';
// Lazy load heavy component
const HeavyComponent = defineAsyncComponent(() =>
import('@/components/HeavyComponent.vue')
);
// With loading/error states
const AsyncComponent = defineAsyncComponent({
loader: () => import('@/components/AsyncComponent.vue'),
loadingComponent: LoadingSpinner,
errorComponent: ErrorDisplay,
delay: 200, // Show loading after 200ms
timeout: 3000 // Error if takes > 3s
});
</script>
<template>
<Suspense>
<template #default>
<HeavyComponent />
</template>
<template #fallback>
<LoadingSpinner />
</template>
</Suspense>
</template>
迁移指南
从 Vue 2 迁移到 Vue 3
| Vue 2 | Vue 3 | 说明 |
|---|---|---|
data() { return {} } | ref(), reactive() | Composition API |
computed: {} | computed(() => {}) | 基于函数的实现 |
watch: {} | watch(), watchEffect() | 显式侦听器 |
mounted() | onMounted() | 从 'vue' 导入 |
this.$emit() | emit() | defineEmits |
props: {} | defineProps<>() | TypeScript 支持 |
| Mixins | Composables | 更好的组合能力 |
$listeners | 合并到 $attrs | 更简洁 |
| Filters | 函数或计算属性 | 已移除 |
从 React 迁移到 Vue 3
| React | Vue 3 | 说明 |
|---|---|---|
useState(0) | ref(0) | 在脚本中需要使用 .value |
useMemo(() => x * 2, [x]) | computed(() => x.value * 2) | 自动跟踪 |
useEffect(() => {}, [x]) | watch(x, () => {}) | 显式依赖项 |
useEffect(() => {}, []) | onMounted() | 生命周期 |
useCallback | 不需要 | 自动保持稳定 |
props.name | props.name | 类似 |
setState(prev => prev + 1) | count.value++ | 直接修改 |
| JSX | Template | 类 HTML 语法 |
最佳实践
- 优先使用 Composition API,而非 Options API,以获得更好的类型推断和组合能力
- 基本类型优先使用
ref(),对象优先使用reactive(),或者在所有场景中都使用ref() - 使用
computed()处理派生状态,而不是使用方法 - 尽早使用
defineProps()解构 props,以确保类型安全 - 使用
<script setup>,减少样板代码并提升性能 - 为 v-for 循环设置唯一 ID 作为 key,确保响应式正常工作
- 优先使用 Pinia,而非 Vuex,以获得更好的 TypeScript 支持和开发者工具
- 延迟加载路由和大型组件,加快初始加载速度
- 使用 composables 提取逻辑,并在组件间复用
- 启用 Vue DevTools,以调试响应式行为和组件树
资源
- Vue 3 文档:https://vuejs.org/guide/introduction.html
- Vue Router:https://router.vuejs.org/
- Pinia:https://pinia.vuejs.org/
- Vite:https://vitejs.dev/
- Vue DevTools:https://devtools.vuejs.org/
- Awesome Vue:https://github.com/vuejs/awesome-vue
总结
- Vue 3 提供 Composition API,包括
setup()、ref()、reactive()、computed()、watch() - 单文件组件(.vue)将模板、脚本和样式整合在一起
- TypeScript 提供一流支持,包括
defineProps<>()和defineEmits<>() - Vue Router 用于客户端路由,支持懒加载和路由守卫
- Pinia 是采用 Composition API 风格的现代状态管理方案
- Vite 提供闪电般快速的开发体验,并支持 HMR
- 组合式函数可提取逻辑,并在各组件间复用
- 渐进式采用,可从简单场景逐步扩展到复杂场景
评论
支持 Markdown