GSP.DEV
  • about
    • ✌️ สวัสดีครับ 😊
  • ⬜introduction
    • 🌐Web Development
      • 🖼️Frontend Developer
      • 🖥️Backend Developer
      • 🎓Full Stack Developer
      • 🚀DevOps Engineer
  • 📖Software
    • 🌐Web Development
      • Language
        • Javascript
        • TypeScript
        • PHP
        • Python
      • Runtime
        • Javascript Runtime
          • Node.js
          • Bun
      • Frontend Framework
        • CSS Framework
          • Tailwind CSS
        • Vite
        • Vue.js
          • Global API
          • Composition API
          • Single-File Component (SFC)
        • Nuxt.js
        • React.js
          • ตัวอย่างการใช้งาน React JS
        • Next.js
      • Backend Framework
        • Express.js
        • NestJS
        • Laravel
    • 🖥️Opeating System
      • Linux
        • Debian
        • Ubuntu
    • 📚Database
      • Relational Databases
        • MySQL
        • Postgres
      • Non-relational Database
        • Document
          • MongoDB
        • Key-value
          • Redis
        • Time Series
          • Prometheus
    • ⚙️Container
      • Docker
        • คำสั่ง Docker พื้นฐาน
      • LXC (Linux Containers)
        • การใช้งาน LXC
        • การใช้งาน Proxmox LXC
      • Container Runtime
        • Docker Container Runtime
        • Containerd
      • Container Orchestration
        • Docker Swarm
        • Kubernetes
    • 🛡️VPN
      • Wireguard
      • Tailscale
    • ☁️Serverless
      • Amazon S3 + Cloudfront
      • Vercel
      • Cloudflare Pages
      • AWS Lambda
    • 📦CDN
      • Cloudfront
    • 🗃️Storage
      • Amazon S3
      • MinIO
    • ✏️Content Management System
      • WordPress
    • 📷Photo Management Software
      • Immich
  • 🏭Infrastructure
    • 🌐Network
      • Firewall
      • Subnet
      • Router
      • Internet
      • Domain
      • DNS
      • Private Network
      • SDN
      • IPAM
      • Protocol
        • IP Address
        • DHCP
        • RTMP
        • TCP
        • UDP
        • HTTP
        • HTTPS
  • ⚙️Technology
    • Cloud Computing คืออะไร
    • VPN คืออะไร
    • Container
  • 🗣️เรื่องเล่า
    • การลดขนาด Image Docker
      • สำหรับ Next.js
    • รีวิว Github Copiot
  • 👨‍💻ลงโค้ด
    • Vue 3
      • การใช้งาน ref
      • การใช้งาน script setup
      • การใช้งาน await ใน script setup
Powered by GitBook
On this page
  • Vue Script Setup
  • ตัวอย่างการใช้งาน
  • เปรียบเทียบ
  • ข้อดีของ <script setup>
  • ข้อเสียของ <script setup>
  • สรุป
  • อ้างอิง

Was this helpful?

  1. ลงโค้ด
  2. Vue 3

การใช้งาน script setup

Vue Script Setup

<script setup> คือไวยากรณ์พิเศษใน Vue 3 ที่ช่วยให้การเขียน component กระชับและง่ายขึ้น โดยรวม script และ template ไว้ในไฟล์เดียวกัน บล็อก <script setup> จะไม่ต้องการการเรียก export default ทำให้โค้ดสะอาดและเข้าใจง่าย

ตัวอย่างการใช้งาน

<template>
  <button @click="increaseCount">Count: {{ count }}</button>
</template>

<script setup>
import { ref } from 'vue'

const count = ref(0)
const increaseCount = () => count.value++
</script>

เปรียบเทียบ

  • Traditional Script: ใช้ export default และมักจะมีโครงสร้างที่ซับซ้อนกว่า

  • Script Setup: ใช้สคริปต์แนว declarative ทำให้สั้นและอ่านง่ายขึ้น ประสิทธิภาพดีกว่าเพราะประมวลผลขณะ compile

ข้อดีของ <script setup>

  • ลดความซับซ้อนของโค้ดด้วยการไม่ต้องใช้ export default

  • ตัวโค้ดอ่านและเข้าใจได้ง่ายขึ้น

  • ประสิทธิภาพดีขึ้นเนื่องจากมีการประมวลผลขณะ compile

ข้อเสียของ <script setup>

  • อาจไม่รองรับกับโค้ดหรือโปรเจกต์ที่ใช้โครงสร้างแบบเก่า

  • ฟีเจอร์บางอย่างเช่น TypeScript types จำเป็นต้องพิจารณาการใช้งานให้ดีเพื่อให้ทำงานร่วมกันได้อย่างถูกต้อง

สรุป

<script setup> เป็นแนวทางใหม่ในการเขียนโค้ด Vue ที่ให้ความกระชับและความชัดเจนในโค้ด โดยลดความซับซ้อนและเพิ่มประสิทธิภาพการประมวลผล อย่างไรก็ตาม ควรพิจารณาความเข้ากันได้กับโค้ดเก่าหรือการใช้ TypeScript อย่างระมัดระวังเพื่อความถูกต้องในการทำงานร่วมกันในโปรเจกต์ต่างๆ.

อ้างอิง

Previousการใช้งาน refNextการใช้งาน await ใน script setup

Last updated 4 months ago

Was this helpful?

👨‍💻
sfc-script-setup basic-syntax