Cú pháp Template
Văn bản & Biểu thức
{{ message }} {{ count + 1 }} {{ ok ? 'Yes' : 'No' }}
Directives
{{ expr }}Nội suy văn bản
v-bind:attr / :attrGắn thuộc tính vào biểu thức
v-on:event / @eventGắn trình lắng nghe sự kiện
v-modelBinding hai chiều (form)
v-if / v-else-if / v-elseRender có điều kiện
v-showBật/tắt display CSS (vẫn giữ trong DOM)
v-forRender danh sách
v-slot / #nameNội dung slot có tên
Binding thuộc tính
Reactivity
ref (giá trị nguyên thủy)
import { ref } from 'vue' const count = ref(0) console.log(count.value) // 0 count.value++ // reactive update
reactive (đối tượng)
import { reactive } from 'vue' const state = reactive({ count: 0, name: 'Vue' }) state.count++ // no .value needed
ref vs reactive
ref()Mọi kiểu; truy cập bằng .value trong script
reactive()Chỉ object/array; truy cập thuộc tính trực tiếp
TemplateCả hai tự động unwrap (không cần .value)
Destructurereactive mất reactivity khi destructure; dùng toRefs()
Computed & Watchers
Computed
import { ref, computed } from 'vue' const items = ref([1, 2, 3, 4, 5]) const evenItems = computed(() => items.value.filter(n => n % 2 === 0) )

Giá trị computed được cache, chỉ tính lại khi dependency thay đổi

Watchers
import { ref, watch, watchEffect } from 'vue' const query = ref('') // Watch specific source watch(query, (newVal, oldVal) => { console.log(`Changed: ${oldVal} → ${newVal}`) }) // Auto-track dependencies watchEffect(() => { console.log(`Query is: ${query.value}`) })
Tùy chọn Watch
immediate: trueChạy callback ngay khi tạo
deep: trueTheo dõi sâu đối tượng lồng nhau
flush: 'post'Chạy sau khi DOM cập nhật
once: trueChỉ kích hoạt một lần rồi dừng
Components
Single File Component (SFC)
Đăng ký Component
Các khối SFC
<script setup>Composition API (khuyến nghị)
<template>Template HTML
<style scoped>CSS theo phạm vi component
<style module>CSS module (đối tượng $style)
Props & Events
Định nghĩa Props
Emit Events
Dùng trong component cha
v-model trong Component
Slots
Slot mặc định

Custom content here

Slot có tên

Main content

Scoped Slots
Composition API
Hàm Composable
// 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 } }
Dùng Composable
provide / inject
// Parent import { provide, ref } from 'vue' const theme = ref('dark') provide('theme', theme) // Descendant (any depth) import { inject } from 'vue' const theme = inject('theme', 'light') // default
Router (Vue Router)
Định nghĩa routes
import { createRouter, createWebHistory } from 'vue-router' const router = createRouter({ history: createWebHistory(), routes: [ { path: '/', component: Home }, { path: '/about', component: About }, { path: '/user/:id', component: User }, ] })
Điều hướng trong Template
Home User 1
Điều hướng bằng code
import { useRouter, useRoute } from 'vue-router' const router = useRouter() const route = useRoute() router.push('/about') router.push({ name: 'user', params: { id: 1 } }) console.log(route.params.id)
Tính năng Route
/user/:idSegment động (route.params.id)
name: 'user'Route có tên để điều hướng bằng code
children: [...]Routes lồng nhau
beforeEnterNavigation guard theo route
meta: { auth: true }Metadata tùy chỉnh cho guards
redirect: '/new-path'Chuyển hướng route
Lifecycle Hooks
Thứ tự Hooks
onBeforeMountTrước khi render DOM lần đầu
onMountedDOM sẵn sàng (fetch data, thêm listeners)
onBeforeUpdateTrước khi re-render do state thay đổi
onUpdatedSau khi re-render DOM
onBeforeUnmountTrước khi component bị hủy
onUnmountedDọn dẹp (xóa listeners, timers)
Cách dùng
Danh sách & Điều kiện
v-for
  • {{ item.name }}
  • {{ index }}: {{ item.name }}
  • {{ key }}: {{ val }}

    Luôn dùng :key trong v-for để cập nhật DOM hiệu quả

    v-if vs v-show
    v-ifRender có điều kiện (thêm/xóa khỏi DOM)
    v-else-ifChuỗi else-if
    v-elseNhánh dự phòng
    v-showBật/tắt display: none (vẫn giữ trong DOM)

    Dùng v-show khi chuyển đổi thường xuyên, v-if khi ít thay đổi

    Ví dụ điều kiện
    Loading...
    Error!
    {{ data }}
    Xử lý Form
    v-model cơ bản
    Modifier của v-model
    v-model.lazyĐồng bộ khi change thay vì input
    v-model.numberTự động ép kiểu sang số
    v-model.trimTự động xóa khoảng trắng
    Modifier của Event
    @click.preventGọi preventDefault()
    @click.stopGọi stopPropagation()
    @click.onceChỉ kích hoạt tối đa một lần
    @keyup.enterChỉ phím Enter
    @submit.preventNgăn submit form mặc định