vue 实现手册
以下是 Vue.js 的核心实现方法和关键概念整理,涵盖基础到进阶的实用内容:
安装与项目初始化
使用 Vue CLI 或 Vite 创建项目:
npm create vue@latest
# 或
npm create vite@latest my-vue-app --template vue
基础模板语法
数据绑定与指令:
<template>
<div>
<p>{{ message }}</p>
<button @click="handleClick">Click</button>
<input v-model="inputText">
</div>
</template>
组件系统
单文件组件结构:
<script setup>
const props = defineProps(['title'])
const emit = defineEmits(['update'])
</script>
<template>
<h2>{{ title }}</h2>
</template>
状态管理
Pinia 基础用法:
// stores/counter.js
import { defineStore } from 'pinia'
export const useCounterStore = defineStore('counter', {
state: () => ({ count: 0 }),
actions: {
increment() {
this.count++
}
}
})
路由配置
Vue Router 示例:
import { createRouter, createWebHistory } from 'vue-router'
const routes = [
{ path: '/', component: Home },
{ path: '/about', component: About }
]
const router = createRouter({
history: createWebHistory(),
routes
})
生命周期钩子
常用生命周期:
<script setup>
import { onMounted, onUpdated } from 'vue'
onMounted(() => {
console.log('组件挂载完成')
})
onUpdated(() => {
console.log('组件更新完成')
})
</script>
组合式API
响应式数据管理:
<script setup>
import { ref, reactive, computed } from 'vue'
const count = ref(0)
const user = reactive({ name: 'John' })
const doubleCount = computed(() => count.value * 2)
</script>
性能优化
关键优化策略:
- 使用
v-once静态内容缓存 - 长列表采用
vue-virtual-scroller - 组件懒加载:
defineAsyncComponent - 状态持久化:
pinia-plugin-persistedstate
测试方案
单元测试示例(Vitest):
import { mount } from '@vue/test-utils'
import MyComponent from './MyComponent.vue'
test('emits event', () => {
const wrapper = mount(MyComponent)
wrapper.find('button').trigger('click')
expect(wrapper.emitted('submit')).toBeTruthy()
})
部署构建
生产环境构建命令:

npm run build
# 生成 dist 目录部署到任意静态服务器
常用插件推荐
- UI库:Element Plus/Quasar/PrimeVue
- 表单处理:VeeValidate
- 动画:Motion One
- 图表:ECharts for Vue
以上内容覆盖了 Vue 3 的主要技术栈,实际开发中可根据项目需求选择合适的技术组合。官方文档(vuejs.org)始终是最权威的参考资料。






