🎯 学习目标
通过本文,你将深入理解Vue3动画效果的核心原理和实践应用,掌握以下关键技能:
- Transition的基本概念和工作机制
- TransitionGroup的进阶技巧和最佳实践
- GSAP的实际应用和性能优化 预计学习时间:12小时
💡 核心概念详解
Vue3动画效果是Vue开发中的关键知识点,掌握这些技能可以构建高质量的Vue应用。
基础用法
<script setup>
import { ref, computed } from 'vue';
const count = ref(0);
const doubled = computed(() => count.value * 2);
function increment() {
count.value++;
}
</script>
<template>
<div>
<p>Count: {{ count }} (doubled: {{ doubled }})</p>
<button @click="increment">Increment</button>
</div>
</template>
进阶用法
<script setup>
import { ref, reactive, watch, onMounted, onUnmounted } from 'vue';
const searchQuery = ref('');
const searchResults = ref([]);
const isLoading = ref(false);
let debounceTimer = null;
watch(searchQuery, (newVal) => {
if (debounceTimer) clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
performSearch(newVal);
}, 300);
});
async function performSearch(query) {
if (!query.trim()) {
searchResults.value = [];
return;
}
isLoading.value = true;
try {
const response = await fetch(`/api/search?q=${encodeURIComponent(query)}`);
searchResults.value = await response.json();
} catch (error) {
console.error('Search failed:', error);
} finally {
isLoading.value = false;
}
}
</script>
<template>
<div>
<input
v-model="searchQuery"
placeholder="Search..."
:disabled="isLoading"
/>
<div v-if="isLoading">Loading...</div>
<ul v-else>
<li v-for="result in searchResults" :key="result.id">
{{ result.title }}
</li>
</ul>
</div>
</template>
实战场景
<script setup>
import { ref, provide, inject } from 'vue';
const ThemeSymbol = Symbol('theme');
function ThemeProvider({ children }) {
const theme = ref('light');
function toggleTheme() {
theme.value = theme.value === 'light' ? 'dark' : 'light';
}
provide(ThemeSymbol, { theme, toggleTheme });
return children;
}
function ThemeButton() {
const { theme, toggleTheme } = inject(ThemeSymbol);
return (
<button onClick="toggleTheme">
Current theme: {theme}
</button>
);
}
</script>
<template>
<ThemeProvider>
<ThemeButton />
</ThemeProvider>
</template>
📖 深入理解Transition
Transition是Vue3的核心响应式系统。
核心原理
Vue3使用Proxy代理实现响应式,当数据变化时自动更新视图。
关键特性
- ref: 创建响应式基本类型
- reactive: 创建响应式对象
- computed: 计算属性
- watch: 侦听器
常见误区
- 直接赋值: 直接替换reactive对象会失去响应性
- 解构问题: 解构reactive对象会失去响应性
- 数组索引: 通过索引修改数组需要特殊处理
📖 掌握TransitionGroup
TransitionGroup是Vue3的组合式API。
优势对比
| 方案 | 优点 | 缺点 | 适用场景 |
|---|---|---|---|
| Options API | 易于理解 | 代码分散 | 简单组件 |
| Composition API | 逻辑聚合 | 学习曲线 | 复杂组件 |
最佳实践
- 逻辑复用: 将可复用逻辑提取为composables
- 代码组织: 按功能组织代码
- 类型支持: 配合TypeScript使用
📖 实践GSAP
GSAP是Vue3状态管理的重要内容。
Pinia使用示例
import { defineStore } from 'pinia';
export const useCounterStore = defineStore('counter', {
state: () => ({
count: 0
}),
getters: {
doubled: (state) => state.count * 2
},
actions: {
increment() {
this.count++;
}
}
});
状态持久化
使用pinia-plugin-persistedstate实现状态持久化。
⚖️ 方案对比
| 方案 | 优点 | 缺点 | 适用场景 |
|---|---|---|---|
| Pinia | 轻量简洁 | 生态较小 | Vue3项目 |
| Vuex | 功能完整 | 复杂 | Vue2项目 |
📚 推荐学习资源
- Vue官方文档 — 官方指南
- Vue Mastery — 视频教程
- Vue School — 学习平台