Ứng dụng thật được tổ chức thành nhiều component, mỗi component là một tệp .vue gồm template, script và style. Bài này tạo dự án bằng công cụ chính thức create-vue và tìm hiểu cấu trúc tệp .vue.

45 phútTrung cấp
Bạn sẽ học được
  • Tạo dự án bằng npm create vue
  • Hiểu cấu trúc tệp .vue
  • Dùng script setup
  • Chạy dev và build

Tạo dự án

npm create vue@latest
# Đặt tên qlsv-vue, chọn Router, Pinia nếu cần
cd qlsv-vue
npm install
npm run dev

Tệp .vue

<!-- src/components/TheSinhVien.vue -->
<script setup>
import { ref } from "vue";
const dem = ref(0);
</script>

<template>
  <div class="the">
    <button @click="dem++">Thích ({{ dem }})</button>
  </div>
</template>

<style scoped>
.the { padding: 12px; border: 1px solid #ddd; }
</style>
  • <script setup>: cách viết gọn, biến khai báo dùng được ngay trong template.
  • scoped: CSS chỉ áp dụng cho component này.

Cấu trúc thư mục

Thư mục/tệpVai trò
src/main.jsTạo app, gắn router, pinia, mount
src/App.vueComponent gốc
src/components/Component dùng lại
src/views/Các trang (khi dùng Router)
Mẹo hay

Từ bài này trở đi dùng Composition API với script setup; cách viết data/methods ở Bài 1–4 gọi là Options API, vẫn hợp lệ.

Thực hành
  1. Tạo dự án qlsv-vue.
  2. Tạo component TheSinhVien.vue và dùng trong App.vue.
  3. Chạy npm run build và xem thư mục dist/.
Tóm tắt bài học
  • create-vue tạo dự án với Vite.
  • Tệp .vue gồm template, script, style.
  • script setup là cách viết khuyến nghị.