ข้ามไปที่เนื้อหาหลัก

Vue.js ทำงานอย่างไร

 Vue.js เป็น JavaScript Framework ที่ใช้สำหรับการพัฒนา UI (User Interface) และ Single Page Applications (SPA) โดยมุ่งเน้นความง่ายและยืดหยุ่นในการใช้งาน ซึ่งทำงานได้อย่างมีประสิทธิภาพด้วยแนวคิดหลักดังนี้:


1. Declarative Rendering


Vue.js ใช้แนวคิดการเชื่อมโยงข้อมูล (Data Binding) ระหว่าง View (UI) และ Model (Data) ด้วย {{ Mustache Syntax }} หรือ v-bind ทำให้เมื่อข้อมูลเปลี่ยน UI จะอัปเดตอัตโนมัติ


ตัวอย่าง


<div id="app">

  <p>{{ message }}</p>

</div>


<script>

  const app = Vue.createApp({

    data() {

      return {

        message: "Hello, Vue!"

      };

    }

  });

  app.mount('#app');

</script>


เมื่อ message เปลี่ยนแปลง ค่าใน UI จะเปลี่ยนตามโดยอัตโนมัติ


2. Component-Based Architecture


Vue แบ่งแอปพลิเคชันออกเป็นส่วนย่อย (Components) เพื่อให้สามารถนำกลับมาใช้ซ้ำและจัดการโค้ดได้ง่ายขึ้น


ตัวอย่าง


Vue.component('my-component', {

  template: `<p>This is a custom component!</p>`

});


3. Virtual DOM


Vue ใช้ Virtual DOM เพื่อเพิ่มประสิทธิภาพในการอัปเดต UI โดยเปรียบเทียบความเปลี่ยนแปลงระหว่าง Virtual DOM และ DOM จริง ก่อนจะทำการเปลี่ยนแปลง DOM จริงเฉพาะจุดที่จำเป็น


4. Reactive Data Binding


Vue ใช้ระบบ Reactive Data System โดยการติดตามการเปลี่ยนแปลงในข้อมูล และอัปเดต UI โดยอัตโนมัติด้วย Observer Pattern


ตัวอย่าง


data() {

  return {

    counter: 0

  };

}

methods: {

  increment() {

    this.counter++;

  }

}


เมื่อ counter เปลี่ยนค่า UI จะเปลี่ยนแปลงตามทันที


5. Directives


Vue มีคำสั่งพิเศษที่ช่วยในการจัดการ DOM เช่น v-if, v-for, v-model, v-on เป็นต้น


ตัวอย่าง


<div v-if="isVisible">Visible!</div>


ถ้าค่า isVisible เป็น true จะโชว์ข้อความ “Visible!”


6. Template Syntax


Vue ใช้ Template-Based Syntax เพื่อทำให้การเขียน UI และการผสานกับ JavaScript ง่ายขึ้น


ตัวอย่าง


<input v-model="text" />

<p>{{ text }}</p>


ข้อมูลใน v-model จะเชื่อมโยงกับ UI และปรับเปลี่ยนทันที


7. State Management


Vue ใช้ Vuex สำหรับจัดการ State ในแอปพลิเคชันขนาดใหญ่ เพื่อให้ข้อมูลทั่วทั้งแอปพลิเคชันสอดคล้องกัน


สรุปการทำงานของ Vue.js

1. กำหนดข้อมูลใน data(): Vue จะสร้างระบบติดตามข้อมูล (Reactivity System)

2. เชื่อมโยงข้อมูลกับ UI: ด้วย Template Syntax หรือ Directives

3. ใช้ Virtual DOM: ในการจัดการการเปลี่ยนแปลงของ UI

4. ปรับแต่งโครงสร้างด้วย Components: ทำให้โค้ดสะอาดและจัดการได้ง่ายขึ้น


Vue.js จึงเป็น Framework ที่เหมาะสำหรับงานพัฒนา UI และ SPA ที่ต้องการความยืดหยุ่นและง่ายต่อการจัดการโค้ด!


ความคิดเห็น

โพสต์ยอดนิยมจากบล็อกนี้

SaaS API-Base Definition, Benefits, Challenges, Problems and Goal for Innovation

What is an Application Programming Interface? API is a set of protocols, standards, and tools that allow two or more software applications to connect and share specific data. API  What is API-Base Saas? API-based SaaS is a software application hosted in the cloud. Users and other programs can access the software’s features, data, and functions via an API instead of a user interface. API refers to the software delivery model as a SaaS Application's functionalist and features are exposed and made to customers through APIs. This combination of the business model of technology on a cloud-base.   This is an integration Service on the cloud provider The Benefits of API-Base SaaS User Experience  Simplifies Development  Increases Accessibility Flexible and Scalable  The Challenges of API-Base SaaS Startup Performance  Integration  Security Pricing What’s The Difference Between SaaS And An API? RPC APIs.  WebSocket APIs. SOAP APIs. REST APIs. The Too...

Interactive เทคโนโลยีเชิงโต้ตอบเปลี่ยนโลกเข้าสู่ยุค Metaverse

เทคโนโลยี Interactive คืออะไร คำนิยามของ เทคโนโลยี Interactive ที่สร้างและพัฒนาขึ้นสำหรับโปรแกรมหรือแอพลิเคชั่นที่เป็น Real-Time เรียกง่ายๆว่า Real-Time Programming (RTP) โดยเน้นไปยังผู้ใช้หรือมนุษย์นั้นเอง จะประกอบไปด้วย 2 องค์ประกอบ คือ ส่วนแรก เทคโนโลยี Interactive เข้าทำการเปลี่ยนแปลรูปร่าง ขนาด และรูปแบบ ซึ่งมาจาก web service อุปกรณ์ sensor ผ่านคอมพิวเตอร์ และมือถือ เป็นผสมผสานระหว่างระบบดิจิทัลและแอนนาล๊อกเข้าด้วยกัน ผ่านปุ่ม สไลด์เดอร์ หรือสวิทซ์ เพื่อการควบคุมในส่วนควบคุมทั้งหมด ที่เรียกว่า Control panel  ส่วนที่ 2  แอพลิเคชั่นที่ทำงานแบบ Real Time Application ถูกออกแบบในแนวคิดหลักของเทคโนโลยี Real Time  อ้างอิง https://interactiveimmersive.io/blog/beginner/02-interactive-technology/ เทคโนโลยีเชิงโต้ตอบที่เน้นการสร้างต้นแบบที่เร็วขึ้น มีประโยชน์อย่างมากในแอปพลิเคชันและประสบการณ์การสร้างต้นแบบ  ประเภทของเทคโนโลยี Interactive  มี 6 ประเภท 1. IoT เป็นแนวคิดจะเปลี่ยนบริการทุกบริการให้อยู่บนเครือข่ายอินเทอร์เน็ต และสามารถเชื่อมต่อเข้ากับแอพลิเคชั่นทุกแอ...