Vue 前端开发
难度等级:⭐⭐⭐ 前置知识:编程语言基础、Web 基础 后续衔接:架构设计、工程化
学习路径
- 入门阶段:掌握 Vue 实例、组件基础、模板语法、响应式数据
- 进阶阶段:理解 Composition API、状态管理、路由、组件通信
- 精通阶段:能够进行大型 Vue 应用架构设计、性能优化、工程化配置
一、Vue 核心概念
1.1 响应式原理
Vue 的核心特性是响应式系统,能够自动追踪数据变化并更新视图。Vue 3 使用 Proxy 实现响应式,相比 Vue 2 的 Object.defineProperty 有显著改进。
核心要点
- Proxy 可以拦截对象的任何属性访问和修改,包括属性增删、数组索引操作
- 依赖收集:访问属性时通过 track() 将当前 effect 添加到依赖集合
- 触发更新:修改属性时通过 trigger() 执行所有依赖的 effect
- 惰性代理:嵌套对象仅在访问时才被代理,提升性能
- shallowRef/shallowReactive 可创建浅层响应式对象,减少代理开销
代码示例
import { ref, reactive, computed, watch } from 'vue'
// ref 用于基本类型
const count = ref(0)
count.value++ // 触发更新
// reactive 用于对象
const state = reactive({
user: { name: 'Vue', age: 10 },
items: [1, 2, 3]
})
state.user.name = 'Vue 3' // 触发更新
// computed 缓存计算结果
const doubleCount = computed(() => count.value * 2)
// watch 监听变化执行副作用
watch(count, (newVal, oldVal) => {
console.log(`Count changed from ${oldVal} to ${newVal}`)
})
关联知识点:Proxy、依赖收集、发布-订阅模式
1.2 组件系统
组件是 Vue 应用的基本构建块,每个组件封装了模板、逻辑和样式。
核心要点
- 单文件组件(.vue 文件)包含 template、script、style 三部分
- Vue 3 推荐使用
<script setup>语法,代码更简洁、类型推断更好 - 组件实例在 setup 阶段创建,替代了 beforeCreate 和 created 生命周期
- 组件可复用、可组合,形成组件树
- Props 向下传递,Events 向上传递,形成单向数据流
代码示例
<!-- ProductCard.vue -->
<script setup lang="ts">
interface Props {
product: {
id: number
name: string
price: number
}
discount?: number
}
const props = withDefaults(defineProps<Props>(), {
discount: 0
})
const emit = defineEmits<{
add: [product: Props['product']]
remove: [id: number]
}>()
const finalPrice = computed(() => {
return props.product.price * (1 - props.discount / 100)
})
</script>
<template>
<div class="product-card">
<h3>{{ product.name }}</h3>
<p>价格: ¥{{ finalPrice }}</p>
<button @click="emit('add', product)">加入购物车</button>
</div>
</template>
关联知识点:单向数据流、组件通信、TypeScript
1.3 生命周期
Vue 组件从创建到销毁经历一系列生命周期钩子,开发者可在特定阶段执行逻辑。
核心要点
- setup:组件实例创建时执行,替代 beforeCreate/created
- onBeforeMount/onMounted:DOM 挂载前后,Mounted 后可安全操作 DOM
- onBeforeUpdate/onUpdated:数据变化触发重新渲染前后
- onBeforeUnmount/onUnmounted:组件销毁前后,用于清理资源
- onErrorCaptured:捕获子组件异常
- onRenderTracked/onRenderTriggered:调试响应式依赖
代码示例
import { onMounted, onUnmounted, ref } from 'vue'
export function useWindowSize() {
const width = ref(window.innerWidth)
const height = ref(window.innerHeight)
function updateSize() {
width.value = window.innerWidth
height.value = window.innerHeight
}
onMounted(() => {
window.addEventListener('resize', updateSize)
})
onUnmounted(() => {
window.removeEventListener('resize', updateSize)
})
return { width, height }
}
关联知识点:DOM 操作、资源清理、自定义 Hooks
二、Composition API 详解
2.1 组合式函数(Composables)
组合式函数是 Composition API 的核心,用于封装和复用状态逻辑。
核心要点
- 以 use 开头的函数,内部可调用其他 Vue API
- 每次调用获得独立的状态实例,避免 mixin 的命名冲突
- 可组合多个组合式函数形成更复杂的逻辑
- 替代了 Vue 2 的 mixin,逻辑来源清晰
代码示例
// useFetch.ts
export function useFetch<T>(url: MaybeRef<string>) {
const data = ref<T | null>(null)
const error = ref<Error | null>(null)
const loading = ref(true)
watch(url, async (newUrl) => {
loading.value = true
try {
const response = await fetch(newUrl)
if (!response.ok) throw new Error(response.statusText)
data.value = await response.json()
} catch (e) {
error.value = e as Error
} finally {
loading.value = false
}
}, { immediate: true })
return { data, error, loading }
}
// 使用
const { data, loading } = useFetch<User>('/api/user')
关联知识点:逻辑复用、响应式编程、自定义 Hooks
2.2 Teleport 和 Suspense
Vue 3 新增的内置组件,解决特定场景问题。
核心要点
- Teleport 将组件 DOM 渲染到指定位置,逻辑仍属于当前组件
- Suspense 控制异步组件的加载状态,提供 fallback 内容
- Teleport 常用于模态框、通知等需要突破父元素的场景
- Suspense 实验性特性,未来可能调整 API
代码示例
<!-- Teleport 示例 -->
<template>
<button @click="showModal = true">打开模态框</button>
<Teleport to="body">
<Modal v-if="showModal" @close="showModal = false">
<p>模态框内容渲染到 body 下</p>
</Modal>
</Teleport>
</template>
<!-- Suspense 示例 -->
<template>
<Suspense>
<template #default>
<AsyncComponent />
</template>
<template #fallback>
<div>加载中...</div>
</template>
</Suspense>
</template>
关联知识点:DOM 渲染、异步组件、模态框
三、状态管理
3.1 Pinia
Pinia 是 Vue 3 官方推荐的状态管理库,替代了 Vuex。
核心要点
- 去除了 mutations,只有 state、getters、actions 三个概念
- 完整的 TypeScript 支持,类型推断友好
- 每个 store 独立定义,通过导入其他 store 实现模块间通信
- 支持 DevTools、SSR、代码分割
- 体积小巧(~1KB),API 简洁
代码示例
// stores/user.ts
import { defineStore } from 'pinia'
export interface User {
id: number
name: string
role: string
}
export const useUserStore = defineStore('user', () => {
const user = ref<User | null>(null)
const token = ref<string | null>(null)
const isLoggedIn = computed(() => !!user.value && !!token.value)
const userRole = computed(() => user.value?.role ?? 'guest')
async function login(credentials: { email: string; password: string }) {
const response = await api.login(credentials)
user.value = response.user
token.value = response.token
localStorage.setItem('token', response.token)
}
function logout() {
user.value = null
token.value = null
localStorage.removeItem('token')
}
return { user, token, isLoggedIn, userRole, login, logout }
})
关联知识点:状态管理、TypeScript、模块化
3.2 Vuex(遗留项目)
Vuex 是 Vue 2 时代的状态管理方案,新项目推荐使用 Pinia。
核心要点
- 四大概念:state(状态)、getters(派生状态)、mutations(同步修改)、actions(异步操作)
- 模块化通过 modules 组织,支持命名空间
- 类型支持较弱,需要额外封装
- 迁移到 Pinia 成本较低,逻辑可逐步转换
关联知识点:状态管理、迁移策略
四、Vue Router
4.1 路由基础
Vue Router 是 Vue 官方路由管理器,支持单页应用的路由切换。
核心要点
- 声明式导航:
<router-link>组件,编程式导航:router.push() - 动态路由:
/user/:id通过 route.params.id 获取 - 嵌套路由:children 配置子路由
- 路由懒加载:
() => import('./views/Home.vue')按需加载 - 路由守卫:全局、路由独享、组件内三级守卫
代码示例
// router/index.ts
import { createRouter, createWebHistory } from 'vue-router'
const routes = [
{
path: '/',
component: () => import('@/views/Home.vue'),
meta: { requiresAuth: false }
},
{
path: '/dashboard',
component: () => import('@/views/Dashboard.vue'),
meta: { requiresAuth: true, roles: ['admin', 'user'] }
}
]
const router = createRouter({
history: createWebHistory(),
routes
})
router.beforeEach((to) => {
const userStore = useUserStore()
if (to.meta.requiresAuth && !userStore.isLoggedIn) {
return '/login'
}
})
export default router
关联知识点:单页应用、权限控制、懒加载
五、前端设计模式在 Vue 中的应用
5.1 Observer(观察者模式)
Vue 的响应式系统本质上是观察者模式的实现。
应用场景
- 响应式数据变化时,自动更新视图和触发 watch 回调
- EventBus 实现组件间通信(Vue 3 中推荐使用 mitt 库)
- 状态管理中 store 变化触发组件更新
代码示例
// 简单 EventBus
import mitt from 'mitt'
export const emitter = mitt()
// 发布事件
emitter.emit('user-updated', { name: 'New Name' })
// 订阅事件
emitter.on('user-updated', (data) => {
console.log('User changed:', data)
})
关联知识点:响应式原理、事件系统
5.2 MVVM 模式
Vue 整体架构遵循 MVVM(Model-View-ViewModel)模式。
应用场景
- Model:数据层(API、Pinia store)
- View:模板层(.vue 文件的 template)
- ViewModel:组件逻辑层(script setup),通过数据绑定连接 Model 和 View
- 双向绑定(v-model)是 MVVM 的典型体现
关联知识点:架构模式、数据绑定
5.3 Factory(工厂模式)
应用场景
- 动态组件创建:根据类型渲染不同组件
- API 响应处理:根据状态码创建不同的错误对象
- 表单字段工厂:根据配置动态生成表单字段组件
代码示例
// 动态组件工厂
const componentMap = {
input: InputField,
select: SelectField,
textarea: TextAreaField,
}
function createFieldComponent(type: string, props: any) {
const Component = componentMap[type]
if (!Component) throw new Error(`Unknown field type: ${type}`)
return h(Component, props)
}
关联知识点:动态组件、代码生成
5.4 Strategy(策略模式)
应用场景
- 表单校验策略:不同字段使用不同校验规则
- 排序策略:根据用户选择切换排序算法
- 支付方式策略:切换支付宝、微信、信用卡支付
代码示例
// 校验策略
const validationStrategies = {
email: (v: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v),
phone: (v: string) => /^1[3-9]\d{9}$/.test(v),
required: (v: string) => v.trim() !== '',
}
function validate(value: string, strategy: string) {
const fn = validationStrategies[strategy]
return fn ? fn(value) : true
}
关联知识点:表单校验、算法切换
5.5 Singleton(单例模式)
应用场景
- Pinia store 实例在整个应用中是单例
- Vue Router 实例全局唯一
- 全局配置对象、日志服务
代码示例
// Vue 应用中 router 和 app 都是单例
const router = createRouter({ ... })
const app = createApp(App)
app.use(router)
app.mount('#app')
关联知识点:全局实例、依赖注入
5.6 Decorator(装饰器模式)
应用场景
- 高阶组件(HOC):包装组件添加额外功能
- 混入(mixin):为组件添加通用逻辑(Vue 3 推荐用组合式函数替代)
- 指令:为元素添加额外行为(v-focus、v-permission)
代码示例
// 组合式函数作为装饰器
function withLoading<T extends (...args: any[]) => Promise<any>>(fn: T) {
const loading = ref(false)
const wrapped = async (...args: Parameters<T>) => {
loading.value = true
try {
return await fn(...args)
} finally {
loading.value = false
}
}
return { wrapped, loading }
}
关联知识点:高阶函数、指令
5.7 Adapter(适配器模式)
应用场景
- 适配不同后端 API 返回的数据格式
- 第三方库与 Vue 组件的接口适配
- 统一不同数据源的结构
代码示例
// API 数据适配器
interface UserDTO {
user_name: string
user_age: number
user_email: string
}
interface User {
name: string
age: number
email: string
}
function adaptUser(dto: UserDTO): User {
return {
name: dto.user_name,
age: dto.user_age,
email: dto.user_email,
}
}
关联知识点:数据转换、接口统一
5.8 Proxy(代理模式)
应用场景
- Vue 3 响应式系统本身就是 Proxy 的应用
- 虚拟代理:图片懒加载、延迟渲染
- 缓存代理:计算结果缓存
关联知识点:响应式原理、性能优化
5.9 Facade(外观模式)
应用场景
- 组合式函数封装复杂逻辑,提供简洁接口
- UI 库封装常用组件,简化使用
- API 服务层统一请求方法
代码示例
// API 外观
export const api = {
user: {
get: (id: number) => http.get(`/users/${id}`),
list: (params: QueryParams) => http.get('/users', { params }),
create: (data: CreateUserDTO) => http.post('/users', data),
update: (id: number, data: UpdateUserDTO) => http.put(`/users/${id}`, data),
remove: (id: number) => http.delete(`/users/${id}`),
}
}
关联知识点:API 设计、封装
5.10 Command(命令模式)
应用场景
- 撤销/重做功能(编辑器、设计工具)
- 操作队列管理
- 按钮点击行为封装
关联知识点:撤销重做、操作队列
5.11 Builder(建造者模式)
应用场景
- 复杂对象构建(如表单配置、查询构建器)
- 链式调用配置
代码示例
// 查询构建器
class QueryBuilder {
private conditions: string[] = []
private orderByFields: string[] = []
private limitValue = 10
where(condition: string) {
this.conditions.push(condition)
return this
}
orderBy(field: string) {
this.orderByFields.push(field)
return this
}
limit(n: number) {
this.limitValue = n
return this
}
build() {
return {
where: this.conditions.join(' AND '),
orderBy: this.orderByFields.join(', '),
limit: this.limitValue,
}
}
}
const query = new QueryBuilder()
.where('status = "active"')
.orderBy('created_at')
.limit(20)
.build()
关联知识点:链式调用、复杂对象构建
5.12 Prototype(原型模式)
通过克隆现有对象创建新对象,避免重复初始化。
应用场景
- 组件配置模板克隆:基于预设模板快速创建相似组件
- 表单配置复制:深拷贝复杂表单配置后微调
- 状态快照:保存和恢复应用状态
代码示例
// 深拷贝组件配置
function cloneComponentConfig<T extends object>(config: T): T {
return structuredClone(config) // 现代浏览器原生方法
}
const baseFormConfig = {
fields: [
{ type: 'input', label: 'Name', required: true },
{ type: 'select', label: 'Role', options: ['admin', 'user'] },
],
submitUrl: '/api/form',
validateOnBlur: true,
}
// 克隆后微调
const editFormConfig = cloneComponentConfig(baseFormConfig)
editFormConfig.fields.push({ type: 'textarea', label: 'Notes' })
关联知识点:对象克隆、配置管理
5.13 Bridge(桥接模式)
将抽象部分与实现部分分离,使它们可以独立变化。
应用场景
- 主题系统:抽象主题接口与具体主题实现分离
- 图表库适配:统一图表 API,底层可切换 ECharts/Chart.js
- 多端适配:同一组件逻辑,不同平台渲染实现
代码示例
// 抽象:图表组件
interface ChartRenderer {
render(data: ChartData): void
update(data: ChartData): void
}
// 实现 A:ECharts 渲染器
class EChartsRenderer implements ChartRenderer {
private chart: echarts.ECharts
render(data: ChartData) { /* ECharts 初始化 */ }
update(data: ChartData) { /* ECharts 更新 */ }
}
// 实现 B:Chart.js 渲染器
class ChartJsRenderer implements ChartRenderer {
private chart: Chart
render(data: ChartData) { /* Chart.js 初始化 */ }
update(data: ChartData) { /* Chart.js 更新 */ }
}
// 桥接:根据配置选择实现
const renderer = useECharts ? new EChartsRenderer() : new ChartJsRenderer()
关联知识点:接口抽象、多端适配
5.14 Composite(组合模式)
将对象组合成树形结构,统一处理单个对象和组合对象。
应用场景
- 树形菜单/文件夹结构:统一处理叶子节点和分支节点
- 表单嵌套:表单组包含子表单组和单个字段
- 权限树:角色权限的层级结构
代码示例
// 菜单项组件(递归组合)
interface MenuItem {
label: string
children?: MenuItem[]
action?: () => void
}
function renderMenu(items: MenuItem[]) {
return items.map(item => (
<div class="menu-item">
<span @click="item.action">{{ item.label }}</span>
{item.children && renderMenu(item.children)} // 递归渲染子树
</div>
))
}
关联知识点:递归组件、树形结构
5.15 Flyweight(享元模式)
共享细粒度对象,减少内存占用。
应用场景
- 虚拟列表:只渲染可见区域的 DOM 节点
- 图标系统:共享图标实例而非重复创建
- 大数据表格:单元格对象复用
代码示例
// 图标享元池
const iconPool = new Map<string, SVGElement>()
function getIcon(name: string): SVGElement {
if (!iconPool.has(name)) {
const svg = createSVGIcon(name) // 创建新图标
iconPool.set(name, svg)
}
return iconPool.get(name)!.cloneNode(true) as SVGElement
}
// 使用享元池避免重复创建相同图标
const userIcon = getIcon('user')
const adminIcon = getIcon('user') // 复用同一图标实例
关联知识点:内存优化、对象池
5.16 Iterator(迭代器模式)
提供顺序访问集合元素的方法,不暴露内部结构。
应用场景
- 自定义列表组件的遍历
- 分页数据迭代器
- 树形结构遍历(前序、中序、后序)
代码示例
// 分页迭代器
function* paginatedIterator<T>(fetchFn: (page: number) => Promise<T[]>) {
let page = 1
while (true) {
const items = await fetchFn(page)
if (items.length === 0) break
for (const item of items) {
yield item
}
page++
}
}
// 使用
const userIterator = paginatedIterator((page) => fetch(`/users?page=${page}`))
for await (const user of userIterator) {
console.log(user.name)
}
关联知识点:生成器、懒加载
5.17 Mediator(中介者模式)
用中介对象封装一系列对象交互,降低耦合度。
应用场景
- 复杂表单:各字段通过中介者协调验证和提交
- 对话框组件:按钮、输入框、关闭按钮通过中介者交互
- 多组件联动:筛选器、列表、分页组件协同
代码示例
// 表单中介者
class FormMediator {
private components = new Map<string, any>()
register(name: string, component: any) {
this.components.set(name, component)
}
notify(sender: string, event: string) {
if (sender === 'submitBtn' && event === 'click') {
const isValid = Array.from(this.components.values())
.every(c => c.validate?.() ?? true)
if (isValid) this.components.get('api')?.submit()
}
if (sender === 'resetBtn' && event === 'click') {
this.components.forEach(c => c.reset?.())
}
}
}
关联知识点:解耦、事件协调
5.18 Memento(备忘录模式)
在不破坏封装性的前提下,捕获和恢复对象内部状态。
应用场景
- 撤销/重做(编辑器、设计工具)
- 表单草稿自动保存
- 游戏存档机制
代码示例
// 编辑器状态管理
class EditorMemento {
private history: string[] = []
private currentIndex = -1
save(state: string) {
// 删除当前索引之后的历史(新操作覆盖旧撤销栈)
this.history = this.history.slice(0, this.currentIndex + 1)
this.history.push(state)
this.currentIndex++
}
undo(): string | null {
if (this.currentIndex > 0) {
this.currentIndex--
return this.history[this.currentIndex]
}
return null
}
redo(): string | null {
if (this.currentIndex < this.history.length - 1) {
this.currentIndex++
return this.history[this.currentIndex]
}
return null
}
}
关联知识点:撤销重做、状态快照
5.19 State(状态模式)
允许对象在内部状态改变时改变其行为。
应用场景
- 订单状态流转(待支付→已支付→发货→完成)
- 按钮状态(默认、hover、loading、disabled)
- 工作流引擎
代码示例
// 订单状态机
interface OrderState {
pay(): void
ship(): void
complete(): void
cancel(): void
}
class PendingState implements OrderState {
pay() { console.log('→ Paid'); /* 切换到 PaidState */ }
ship() { throw new Error('Cannot ship pending order') }
complete() { throw new Error('Cannot complete pending order') }
cancel() { console.log('→ Cancelled'); /* 切换到 CancelledState */ }
}
class PaidState implements OrderState {
pay() { throw new Error('Already paid') }
ship() { console.log('→ Shipped'); /* 切换到 ShippedState */ }
complete() { throw new Error('Cannot ship paid order') }
cancel() { console.log('→ Refunded'); /* 切换到 RefundedState */ }
}
关联知识点:状态机、业务流
5.20 Interpreter(解释器模式)
给定一个语言,定义它的文法表示,并定义一个解释器。
应用场景
- 自定义表达式解析(如筛选条件表达式)
- 路由规则解析
- 权限表达式解析(如
admin && (read || write))
代码示例
// 简单表达式解释器
interface Expression {
interpret(context: Record<string, any>): boolean
}
class AndExpression implements Expression {
constructor(private left: Expression, private right: Expression) {}
interpret(ctx) { return this.left.interpret(ctx) && this.right.interpret(ctx) }
}
class OrExpression implements Expression {
constructor(private left: Expression, private right: Expression) {}
interpret(ctx) { return this.left.interpret(ctx) || this.right.interpret(ctx) }
}
class RoleExpression implements Expression {
constructor(private role: string) {}
interpret(ctx) { return ctx.roles.includes(this.role) }
}
// 解析 "admin && (read || write)"
const expr = new AndExpression(
new RoleExpression('admin'),
new OrExpression(new RoleExpression('read'), new RoleExpression('write'))
)
expr.interpret({ roles: ['admin', 'read'] }) // true
关联知识点:语法解析、规则引擎
5.21 Visitor(访问者模式)
在不改变对象结构的前提下,定义作用于对象各元素的新操作。
应用场景
- AST 遍历(Vue 模板编译)
- 表单数据收集:不同访问者收集不同数据
- 组件树分析:统计、校验、转换
代码示例
// 组件树访问者
interface ComponentNode {
type: string
props?: Record<string, any>
children?: ComponentNode[]
}
interface Visitor {
visitComponent(node: ComponentNode): void
visitChildren(node: ComponentNode): void
}
class ComponentTraverser {
accept(node: ComponentNode, visitor: Visitor) {
visitor.visitComponent(node)
visitor.visitChildren(node)
node.children?.forEach(child => this.accept(child, visitor))
}
}
// 具体访问者:统计组件数量
class ComponentCounter implements Visitor {
count = 0
visitComponent() { this.count++ }
visitChildren() {}
}
关联知识点:AST、组件分析
六、前端架构模式
6.1 MVC(Model-View-Controller)
MVC 将应用分为三个部分:模型(数据)、视图(UI)、控制器(业务逻辑)。
核心要点
- Model:管理数据和业务逻辑
- View:展示数据,接收用户输入
- Controller:处理用户输入,更新 Model 和 View
- 前端实现:Model(API/Store)、View(模板)、Controller(路由/事件处理)
关联知识点:架构模式、后端 MVC
6.2 MVP(Model-View-Presenter)
MVP 是 MVC 的演进,Presenter 完全隔离 View 和 Model。
核心要点
- View 被动显示,所有逻辑在 Presenter
- Presenter 不直接操作 DOM,通过接口与 View 交互
- 适合复杂业务逻辑的组件(如数据表格、表单)
- 测试友好:Presenter 可独立测试
关联知识点:解耦、测试驱动
6.3 MVVM(Model-View-ViewModel)
Vue 的核心架构模式,通过数据绑定连接 View 和 ViewModel。
核心要点
- ViewModel 暴露数据和命令给 View
- View 通过数据绑定自动同步 ViewModel 状态
- ViewModel 不依赖具体 View 实现,可复用
- 双向绑定简化状态同步,但需注意性能
关联知识点:数据绑定、Vue 架构
6.4 Flux / Redux 模式
单向数据流架构,适合复杂状态管理。
核心要点
- 单向数据流:View → Action → Store → View
- 状态集中管理,可预测
- Vuex 和 Pinia 都借鉴了 Flux 思想
- Redux 在 React 生态中广泛使用,也可用于 Vue
关联知识点:状态管理、单向数据流
七、组件通信模式
7.1 Props 和 Emits
父子组件通信的标准方式,单向数据流。
核心要点
- Props 从父到子,Emits 从子到父
- 支持 TypeScript 类型定义
- 避免直接修改 props,通过事件通知父组件更新
关联知识点:单向数据流、组件通信
7.2 Provide/Inject
祖先组件向后代组件传递数据,无需逐层传递。
核心要点
- 适用于深层组件树共享数据(主题、语言配置)
- 默认非响应式,需用 ref/reactive 包装
- 主要用于组件库内部通信
关联知识点:依赖注入、组件库设计
7.3 状态管理
通过 Pinia 等全局状态管理库共享数据。
核心要点
- 适合跨组件共享的复杂状态
- 支持 DevTools 调试
- 注意避免过度使用,简单场景用 props/emits
关联知识点:Pinia、全局状态
八、性能优化
7.1 渲染优化
核心要点
- 使用 v-show 替代频繁切换的 v-if
- 使用 keep-alive 缓存组件
- 避免 v-for 和 v-if 同元素使用
- 使用 computed 缓存计算结果
- 虚拟列表渲染大数据(vue-virtual-scroller)
关联知识点:虚拟 DOM、缓存策略
7.2 代码分割
核心要点
- 路由懒加载:
() => import('./views/Home.vue') - 异步组件:
defineAsyncComponent(() => import('./Component.vue')) - 动态 import 按需加载模块
关联知识点:懒加载、按需加载
7.3 响应式优化
核心要点
- 使用 shallowRef/shallowReactive 减少深层对象代理开销
- 避免在 template 中使用复杂表达式,用 computed 替代
- 大对象使用 markRaw 标记为非响应式
关联知识点:Proxy、性能调优
八、工程化
8.1 Vite
Vite 是新一代前端构建工具,Vue 官方推荐。
核心要点
- 基于 ES Modules 的按需编译,开发服务器启动快
- 生产环境使用 Rollup 打包
- 支持 HMR(热模块替换)
- 内置 TypeScript、CSS 预处理器支持
- 插件生态丰富
关联知识点:构建工具、开发体验
8.2 TypeScript 集成
Vue 3 使用 TypeScript 重写,类型支持优秀。
核心要点
<script setup lang="ts">启用 TypeScript- defineProps、defineEmits 支持类型定义
- 使用接口定义组件 props 类型
- 使用泛型增强类型安全
关联知识点:类型安全、开发效率
九、测试
9.1 单元测试
使用 Vitest + Vue Test Utils 进行组件单元测试。
核心要点
- Vitest 与 Vite 共享配置,速度快
- Vue Test Utils 提供 mount/shallowMount 方法
- 测试组件渲染、事件触发、props 传递
关联知识点:测试策略、质量保证
9.2 E2E 测试
使用 Cypress 或 Playwright 进行端到端测试。
核心要点
- 模拟用户真实操作流程
- 测试关键业务路径
- 集成到 CI/CD 流程
关联知识点:自动化测试、CI/CD
十、生态系统
10.1 Nuxt.js
Nuxt 是基于 Vue 的服务端渲染框架。
核心要点
- 支持 SSR、SSG、混合渲染
- 文件系统路由,约定优于配置
- 内置 SEO 优化
- 适合内容型网站、电商
关联知识点:SSR、SEO、全栈框架
10.2 VueUse
VueUse 是 Vue 组合式函数集合。
核心要点
- 100+ 常用组合式函数(useMouse、useLocalStorage 等)
- Tree-shakable,按需引入
- 减少重复造轮子
关联知识点:代码复用、工具库
10.3 UI 组件库
- Element Plus:适合后台管理系统
- Ant Design Vue:企业级设计语言
- Vant:移动端组件库
- Naive UI:TypeScript 友好
关联知识点:组件库、开发效率
十一、与其他框架对比
Vue vs React vs Angular
| 特性 | Vue 3 | React 18 | Angular |
|---|---|---|---|
| 学习曲线 | 低 | 中 | 高 |
| 响应式 | 自动追踪(Proxy) | 手动(useState) | 自动(Zone.js) |
| 模板 | HTML 模板 | JSX | HTML 模板 |
| 状态管理 | Pinia | Redux/Zustand | NgRx |
| 路由 | Vue Router | React Router | Router |
| 类型支持 | 优秀 | 优秀 | 优秀 |
| 适用场景 | 中小型项目、快速开发 | 大型项目、灵活需求 | 企业级大型应用 |
关联知识点:技术选型、框架对比
十二、最佳实践
12.1 代码组织
- 按功能模块组织文件,而非按文件类型
- 组件职责单一,保持小巧
- 组合式函数封装可复用逻辑
- 使用 TypeScript 提高代码质量
12.2 性能优化
- 首屏加载优化:代码分割、懒加载
- 避免不必要的渲染:React.memo 类似,使用 shallowRef
- 列表使用稳定 key
- 使用 Vue DevTools Profiler 定位瓶颈
12.3 安全注意事项
- 用户输入使用 v-html 时注意 XSS
- 敏感信息不存储在前端
- 接口调用添加认证 token
- 使用 HTTPS 传输数据
12.4 常见陷阱
- 解构 reactive 对象丢失响应式(使用 toRefs)
- watch 中闭包陷阱读到旧值
- v-for 使用 index 作为 key 导致状态错误
- 组件内未清理定时器/事件监听导致内存泄漏
关联知识点:代码质量、性能、安全