稍候片刻,月出文自明。
<script setup>。入门先掌握单文件组件、模板指令和表单绑定,再理解 ref、reactive、computed 与 watch 的区别。父子组件用 props 和事件通信,用插槽分发内容,用 provide/inject 跨层传值,用组合式函数复用有状态的逻辑。常见坑包括解构导致响应性丢失、用数组下标作 key、在同一元素上混用 v-if 和 v-for,以及卸载时忘记清理副作用。建议先做 TodoList,再按需加入 Vue Router 和 Pinia;API 细节以官方文档为准。
import { createApp } from 'vue'
import App from './App.vue'
createApp(App).mount('#app')
<script setup>
import { ref } from 'vue'
const message = ref('你好,Vue')
</script>
<template>
<h1>{{ message }}</h1>
</template>
<style scoped>
h1 { color: #42b883; }
</style>
<template>
<p>{{ user.name }}</p>
<img :src="avatarUrl" :alt="user.name">
<button @click="handleClick">提交</button>
</template>
<template>
<!-- 对象语法:值为真才添加 -->
<div :class="{ active: isActive, 'text-danger': hasError }"></div>
<!-- 数组语法,可与静态 class 共存 -->
<div class="static" :class="[activeClass, errorClass]"></div>
<div :style="{ color: activeColor, fontSize: size + 'px' }"></div>
</template>
<template>
<input v-model="text">
<input v-model.trim.number="age">
<textarea v-model.lazy="bio"></textarea>
<input type="checkbox" v-model="agree">
<input type="checkbox" v-model="hobbies" value="reading">
<select v-model="city"><option value="bj">北京</option></select>
</template>
<script setup>
import { ref, reactive, computed, watch, watchEffect } from 'vue'
const firstName = ref('张')
const lastName = ref('三')
const fullName = computed(() => firstName.value + lastName.value)
const form = reactive({ keyword: '', page: 1 })
watch(
() => form.keyword,
(keyword) => { form.page = 1 },
{ immediate: true }
)
watchEffect(() => {
console.log('当前关键词:', form.keyword)
})
</script>
<!-- 子组件 Child.vue -->
<script setup>
const emit = defineEmits(['update', 'delete'])
function remove(id) {
emit('delete', id)
}
</script>
<!-- 父组件:监听事件 -->
<Child @delete="handleDelete" />
<!-- 子组件:一个 v-model 对应 modelValue -->
<script setup>
const model = defineModel({ type: String, default: '' })
</script>
<template>
<input v-model="model">
</template>
<!-- 父组件 -->
<MyInput v-model="text" />
<!-- 多个 v-model -->
<UserForm v-model:name="name" v-model:age="age" />
<!-- 子组件 Card.vue -->
<script setup>
defineProps({ item: { type: Object, required: true } })
</script>
<template>
<div class="card">
<header><slot name="header">默认标题</slot></header>
<main><slot :item="item">{{ item.name }}</slot></main>
</div>
</template>
<!-- 父组件:具名插槽 + 作用域插槽 -->
<Card :item="todo">
<template #header>自定义标题</template>
<template #default="{ item }">{{ item.name }}</template>
</Card>
// composables/useMouse.js
import { ref, onMounted, onUnmounted } from 'vue'
export function useMouse() {
const x = ref(0)
const y = ref(0)
function update(e) {
x.value = e.pageX
y.value = e.pageY
}
onMounted(() => window.addEventListener('mousemove', update))
onUnmounted(() => window.removeEventListener('mousemove', update))
return { x, y }
}
// router/index.js
import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import User from '../views/User.vue'
const router = createRouter({
history: createWebHistory(),
routes: [
{ path: '/', component: Home },
{ path: '/user/:id', component: User, props: true },
{ path: '/about', component: () => import('../views/About.vue') }
]
})
export default router
// main.js 中:createApp(App).use(router).mount('#app')
import { defineStore } from 'pinia'
export const useCounterStore = defineStore('counter', {
state: () => ({ count: 0 }),
getters: { double: (state) => state.count * 2 },
actions: {
increment() { this.count++ }
}
})
Vue 基础入门:一份准确的知识点清单
这篇文章把 Vue 3 入门必须掌握的概念按「是什么、怎么用、哪里会踩坑」的顺序梳理一遍。适合已经会 HTML、CSS 和 JavaScript,正准备上手 Vue 的读者;也可以当作查漏补缺的速查表。
一、先对齐版本与基本认知
Vue 是一个用于构建用户界面的渐进式 JavaScript 框架。所谓「渐进式」,是指你可以只用它接管页面上的一个小组件,也可以用它配合路由、状态管理搭出完整的单页应用。
二、创建项目与最小可运行示例
官方脚手架是 create-vue,底层用 Vite 构建。执行后会交互式询问是否需要 TypeScript、Router、Pinia、测试等选项,按需勾选即可。
入口文件负责把根组件挂载到页面上。这是整棵组件树的起点:
一个最小的单文件组件长这样,模板里可以直接使用脚本中声明的变量:
三、单文件组件(SFC)的结构
四、模板语法与常用指令
模板是带 Vue 语法的 HTML,最终会被编译成渲染函数。插值用双大括号,只用于文本内容,不能写在属性里。
常用指令一览
Class 与 Style 绑定
绑定在组件上的 class 和 style 会与组件根元素上的同名属性合并,不会覆盖。
事件修饰符
v-model 与表单
列表渲染的两个关键点
五、响应式核心:ref、reactive、computed、watch
响应式是 Vue 的心脏。理解这几个 API 的边界,比记住语法更重要。
ref 与 reactive
computed 与 watch
六、生命周期钩子
组合式 API 中,钩子以 onXxx 函数形式在 setup 同步执行阶段注册;选项式 API 的 beforeCreate 与 created 对应 setup 本身的执行时机。
Vue 2 中的 beforeDestroy / destroyed 在 Vue 3 已改名为 beforeUnmount / unmounted,迁移时容易遗漏。
七、组件通信
组件之间默认是隔离的,通信方式按关系选择:父子用 props / 事件,跨层级用 provide / inject,全局状态用 Pinia。
props:父传子,单向数据流
自定义事件:子传父
事件命名推荐统一使用 kebab-case(emit('my-event')),Vue 3 不会像 props 那样自动转换事件名大小写;模板里监听未声明的监听器会落到根元素上($attrs)。defineEmits 声明后能获得校验与类型提示。Vue 3 已移除 .native 修饰符。
组件上的 v-model
defineModel 在 Vue 3.4 起稳定,本质是声明 modelValue prop 并 emit update:modelValue。旧写法需要手动写 props.modelValue 与 emit('update:modelValue', value),面试或读老代码时会遇到。
插槽:内容分发
provide / inject 与透传属性
八、组合式函数(Composables)
把有状态的逻辑抽成以 use 开头的函数,内部可以使用 ref、生命周期钩子等,返回值通常是响应式引用。这是组合式 API 最大的收益。
注意:组合式函数必须在 setup 同步执行期间调用,否则生命周期钩子注册会失效。
九、路由与状态管理(入门够用版)
vue-router
Pinia
十、几个实用的性能与调试要点
十一、新手最常踩的坑
十二、小结与学习路径
把上面的内容压缩成一条主线:先会写 SFC 和模板指令,再吃透 ref / reactive / computed / watch 的响应式模型,然后用 props、事件、插槽把组件拼起来,最后补上路由与状态管理。
下一步建议按这个顺序推进:用 create-vue 建一个项目做 TodoList(练列表渲染与表单)→ 抽一个可复用的卡片组件(练 props、插槽)→ 加 vue-router 做多页面(练路由与懒加载)→ 抽一个 useXxx 组合式函数(练逻辑复用)→ 引入 Pinia 管理跨页状态。