
1. Hiểu khái niệm SPA (Single Page Application)
-
SPA là ứng dụng web chỉ có một trang HTML duy nhất, nội dung thay đổi dựa trên JavaScript mà không cần reload lại trang.
-
Vue.js giúp chúng ta xây dựng SPA dễ dàng: quản lý component, dữ liệu, routing, state.
Ví dụ: bạn truy cập /products
hay /cart
thì vẫn chỉ tải 1 file index.html
, Vue sẽ tự hiển thị nội dung khác nhau.
2. Cài đặt dự án bằng Vite
Tạo project
npm create vite@latest vue3-basic cd vue3-basic npm install npm run dev
Chọn Vue
khi Vite hỏi framework.
Cấu trúc cơ bản
-
main.js
: file khởi tạo Vue app -
App.vue
: component gốc -
components/
: chứa các component con
3. Khởi tạo project Vue 3
main.js
import { createApp } from 'vue' import App from './App.vue' createApp(App).mount('#app')
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Vue 3 Basic</title> </head> <body> <div id="app"></div> <script type="module" src="/src/main.js"></script> </body> </html>
4. Template Syntax – Interpolation
Trong Vue, ta có thể chèn dữ liệu vào HTML bằng {{ }}
.
App.vue
<script setup> import { ref } from 'vue' const message = ref('Xin chào Vue 3!') </script> <template> <h1>{{ message }}</h1> </template>