🚀 Vue 3 最佳实践:从零到一构建高性能前端应用
适用人群:前端开发者、Vue.js 爱好者、想提升 Vue 3 开发效率的工程师
关键词:Vue 3、Composition API、Pinia、Vite、性能优化
📌 为什么选择 Vue 3?
自 Vue 3 发布以来,它以更轻量、更高效、更灵活的特点,成为现代前端开发的首选框架之一。相比 Vue 2,Vue 3 具备以下核心优势:
- ✅ Composition API 提升代码复用性
- ✅ 响应式系统优化,减少性能开销
- ✅ Vite 构建工具,提升开发体验
- ✅ Pinia 状态管理,比 Vuex 更轻量
那么,如何高效、优雅、最佳实践地使用 Vue 3?这篇文章将手把手带你从零开始,构建一个高性能 Vue 3 项目 🚀
🔥 环境搭建
1️⃣ 创建 Vue 3 项目
我们使用 create-vue 脚手架:
npm create vue@latest
# 或者
yarn create vue@latest
✅ 选择 TypeScript + Pinia + Vue Router + Vite 作为默认选项
✅ 推荐使用 Vite,相比 Webpack,Vite 启动速度更快,HMR(热重载)更流畅
2️⃣ 目录结构
安装完成后,你的项目目录大致如下:
📂 my-vue-app
┣ 📂 src
┃ ┣ 📂 components
┃ ┣ 📂 views
┃ ┣ 📂 store
┃ ┣ 📂 assets
┃ ┣ 📂 router
┃ ┣ 📜 main.ts
┃ ┣ 📜 App.vue
┣ 📜 index.html
┣ 📜 vite.config.ts
┣ 📜 package.json
✅ components 存放复用组件
✅ views 存放页面组件
✅ store 采用 Pinia 进行状态管理
✅ router 配置 Vue Router
🔥 组件化开发(Composition API)
3️⃣ 使用 setup() 编写组件
在 Vue 3 中,推荐使用 Composition API 组织代码:
<script setup>
import { ref } from 'vue';
const count = ref(0);
const increment = () => count.value++;
</script>
<template>
<button @click="increment">Count: {{ count }}</button>
</template>
✅ ref() 创建响应式数据
✅ setup() 让代码更简洁、逻辑更清晰
4️⃣ Vue 3 中的 watch & computed
✅ computed() 计算属性
<script setup>
import { ref, computed } from 'vue';
const firstName = ref('张');
const lastName = ref('三');
const fullName = computed(() => `${firstName.value} ${lastName.value}`);
</script>
<template>
<p>{{ fullName }}</p>
</template>
✅ 自动依赖追踪,当 firstName 或 lastName 变化时,fullName 也会自动更新
✅ watch() 监听数据变化
<script setup>
import { ref, watch } from 'vue';
const message = ref('Hello');
watch(message, (newValue, oldValue) => {
console.log(`message 变化了: ${oldValue} -> ${newValue}`);
});
</script>
✅ 监听数据变化,适用于异步操作或副作用函数
🔥 Vue Router 4(官方推荐)
Vue 3 采用 Vue Router 4 进行路由管理:
npm install vue-router@4
5️⃣ 配置路由
在 router/index.ts:
import { createRouter, createWebHistory } from 'vue-router';
import Home from '../views/Home.vue';
import About from '../views/About.vue';
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About },
];
const router = createRouter({
history: createWebHistory(),
routes
});
export default router;
在 main.ts 注册路由:
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
createApp(App).use(router).mount('#app');
✅ createRouter() 创建路由
✅ createWebHistory() 使用 HTML5 历史模式(SEO 友好)
🔥 轻量级状态管理(Pinia)
Vue 3 推荐使用 Pinia,它比 Vuex 更简单:
npm install pinia
6️⃣ 创建状态管理
在 store/index.ts:
import { defineStore } from 'pinia';
export const useCounterStore = defineStore('counter', {
state: () => ({ count: 0 }),
actions: {
increment() {
this.count++;
}
}
});
7️⃣ 在组件中使用
<script setup>
import { useCounterStore } from '@/store';
const counter = useCounterStore();
</script>
<template>
<button @click="counter.increment">Count: {{ counter.count }}</button>
</template>
✅ 更直观,无需 mutations
✅ 支持 TypeScript,更强的类型提示
🔥 Vue 3 高级优化技巧
8️⃣ 懒加载(Lazy Loading)
对于大型项目,可以使用路由懒加载,提高首屏加载速度:
const Home = defineAsyncComponent(() => import('../views/Home.vue'));
✅ 按需加载组件,减少初始 JS 体积
9️⃣ 使用 Suspense 处理异步组件
Vue 3 提供了 Suspense 组件,可以在加载组件时显示占位符:
<Suspense>
<template #default>
<AsyncComponent />
</template>
<template #fallback>
<p>Loading...</p>
</template>
</Suspense>
✅ 增强用户体验,减少空白加载问题
🚀 总结
✅ Vue 3 最佳实践
✅ 使用 Composition API 组织代码
✅ Vue Router 4 进行路由管理
✅ Pinia 替代 Vuex,轻量且强大
✅ 使用 懒加载 + Suspense 提升性能
💡 你学会 Vue 3 了吗?
欢迎在评论区分享你的 Vue 3 经验 🚀✨
📌 更多前端技术文章,记得关注! 💡