Vue.js 是由尤雨溪(Evan You)于 2014 年创建的前端框架,是全球最受欢迎的渐进式 JavaScript 框架。Vue 的核心设计理念是 "渐进式"——你可以从简单的 CDN 脚本开始,逐步扩展到完整的单页应用。
Vue.js 的核心定位是 渐进式、易上手的前端框架。它提供了:
Vue 由前 Google 工程师尤雨溪于 2014 年创建,最初是作为 AngularJS 的一个轻量级替代品。尤雨溪希望保留 Angular 的模板语法和双向绑定,同时提供更简洁、更灵活的 API。
<!-- Vue 模板语法 -->
<template>
<div>
<!-- 文本插值 -->
<h1>{{ message }}</h1>
<!-- 属性绑定 -->
<div :class="activeClass">动态类名</div>
<img :src="imageUrl" />
<!-- 事件监听 -->
<button @click="handleClick">点击我</button>
<!-- 双向绑定 -->
<input v-model="inputValue" />
<!-- 条件渲染 -->
<div v-if="isVisible">显示内容</div>
<div v-else>隐藏内容</div>
<!-- 列表渲染 -->
<ul>
<li v-for="item in items" :key="item.id">
{{ item.name }}
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
message: "Hello, Vue!",
activeClass: "active",
imageUrl: "/image.jpg",
inputValue: "",
isVisible: true,
items: [
{ id: 1, name: "Item 1" },
{ id: 2, name: "Item 2" },
]
};
},
methods: {
handleClick() {
this.message = "点击了按钮!";
}
}
};
</script>
<script setup>
import { ref, reactive, computed, watch, onMounted } from "vue";
// 响应式状态
const count = ref(0);
const user = reactive({
name: "Alice",
age: 30
});
// 计算属性
const doubled = computed(() => count.value * 2);
// 侦听器
watch(count, (newVal, oldVal) => {
console.log(`计数从 ${oldVal} 变为 ${newVal}`);
});
// 方法
const increment = () => {
count.value++;
};
// 生命周期
onMounted(() => {
console.log("组件已挂载");
});
// 组合式函数(自定义 Hook)
function useCounter(initial = 0) {
const count = ref(initial);
const increment = () => count.value++;
const decrement = () => count.value--;
return { count, increment, decrement };
}
// 使用组合式函数
const { count: c, increment: inc } = useCounter(10);
</script>
<!-- 父组件 -->
<template>
<ChildComponent
:title="title"
@update="handleUpdate"
/>
</template>
<script setup>
import ChildComponent from "./ChildComponent.vue";
import { ref } from "vue";
const title = ref("父组件传递的数据");
const handleUpdate = (data) => {
console.log("接收到子组件事件:", data);
};
</script>
<!-- 子组件 -->
<template>
<div>
<h2>{{ title }}</h2>
<button @click="emitUpdate">触发事件</button>
</div>
</template>
<script setup>
const props = defineProps({
title: String
});
const emit = defineEmits(["update"]);
const emitUpdate = () => {
emit("update", "来自子组件的数据");
};
</script>
// stores/counter.js
import { defineStore } from "pinia";
export const useCounterStore = defineStore("counter", {
state: () => ({
count: 0,
users: []
}),
getters: {
doubled: (state) => state.count * 2,
},
actions: {
increment() {
this.count++;
},
async fetchUsers() {
const response = await fetch("/api/users");
this.users = await response.json();
}
}
});
// 在组件中使用
<script setup>
import { useCounterStore } from "@/stores/counter";
const counter = useCounterStore();
// 直接使用
console.log(counter.count);
console.log(counter.doubled);
// 调用 action
counter.increment();
counter.fetchUsers();
// 解构(保持响应式)
import { storeToRefs } from "pinia";
const { count, doubled } = storeToRefs(counter);
</script>
// router/index.js
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 },
{ path: "/users/:id", component: UserDetail }
];
const router = createRouter({
history: createWebHistory(),
routes
});
export default router;
// 在组件中使用
<template>
<nav>
<router-link to="/">首页</router-link>
<router-link to="/about">关于</router-link>
</nav>
<router-view />
</template>
<script setup>
import { useRoute, useRouter } from "vue-router";
const route = useRoute();
const router = useRouter();
// 获取路由参数
const userId = route.params.id;
// 编程式导航
const goToUser = (id) => {
router.push(`/users/${id}`);
};
</script>
// pages/index.vue
<template>
<div>
<h1>{{ title }}</h1>
<button @click="count++">{{ count }}</button>
</div>
</template>
<script setup>
// 服务端数据获取
const { data: posts } = await useFetch("/api/posts");
// 页面元数据
useHead({
title: "首页 - Nuxt",
meta: [{ name: "description", content: "描述内容" }]
});
// 状态管理
const count = ref(0);
// SEO 优化
definePageMeta({
layout: "default"
});
</script>
| 对比项 | Vue.js | React | Angular |
|---|---|---|---|
| 类型 | 渐进式框架 | UI 库 | 全栈框架 |
| 学习曲线 | 平缓 | 中等 | 陡峭 |
| 模板语法 | HTML + 指令 | JSX | HTML + 属性 |
| 状态管理 | Pinia/Vuex | Redux/Zustand | RxJS/Service |
| 双向绑定 | ✅ v-model | ❌(需要手动) | ✅ ngModel |
| 中国社区 | 最大 | 大 | 中等 |
| 大厂背书 | 独立 | Meta | |
| 学习建议 | 快速上手 | 全栈首选 | 大型项目 |
JavaScript ES6+(箭头函数、解构、模块、Promise)
模板语法、数据绑定、指令、事件处理、组件基础
Composition API、响应式系统(ref/reactive)、生命周期、组件通信
Vue Router、Pinia 状态管理、Nuxt.js、性能优化、SSR
Vue.js 是前端开发的"入门首选"。
它用 渐进式设计、简洁的 API、完善的生态 让前端开发变得简单而愉悦。Vue 是中国开发者最喜爱的前端框架,也是阿里巴巴、腾讯、字节跳动等大厂的主流选择。
无论你是初学者还是资深开发者,Vue 都是值得深入学习的前端框架。
"Vue 让前端开发变得优雅而高效。" 💚