Binding ใน vue.js
    
    
    
        
            Created At : 
        
    
    
        
        
        
            Views 👀 :
                
            
        
        
        
    
    
    
    
        
    
    
    
        
      
        การ “Binding” ใน Vue.js เป็นกระบวนการที่ใช้ในการเชื่อมโยง (bind) ข้อมูลจาก Vue instance ไปยัง HTML element หรือ attributes เพื่อให้ข้อมูลแสดงผลและอัปเดตอัตโนมัติเมื่อข้อมูลเปลี่ยนแปลง นี่คือหลายตัวอย่างของการ Binding ใน Vue.js:
- Interpolation (การฝังข้อมูล): ใน template ของ Vue, คุณสามารถใช้ Interpolation (การฝังข้อมูล) โดยใช้ 
{{}} สำหรับการแสดงข้อมูล: 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
   | <template>   <div>     <p>{{ message }}</p>   </div> </template>
  <script> export default {   data() {     return {       message: "Hello Vue.js",     };   }, }; </script>
   | 
 
ในตัวอย่างนี้, message จะถูก Binding และแสดงผลใน <p> element.
- Binding Attributes (การเชื่อมโยงแอตทริบิวต์): คุณสามารถ Binding แอตทริบิวต์ของ HTML elements โดยใช้ 
v-bind หรือสั้นๆ คือ :  เช่นการ Binding href attribute ของลิงก์: 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
   | <template>   <div>     <a v-bind:href="url">Visit our website</a>   </div> </template>
  <script> export default {   data() {     return {       url: "https://www.example.com",     };   }, }; </script>
   | 
 
- Binding Class และ Style: คุณสามารถใช้ 
v-bind หรือ : เพื่อ Binding คลาสและสไตล์ CSS ของ elements ได้: 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   | <template>   <div>     <p v-bind:class="{ active: isActive }">This is a paragraph</p>     <button v-bind:style="{ color: textColor }">Change Color</button>   </div> </template>
  <script> export default {   data() {     return {       isActive: true,       textColor: "blue",     };   }, }; </script>
   | 
 
- Two-Way Binding (การ Binding สองทาง): Vue.js ยังมี 
v-model ที่ใช้สำหรับการ Binding สองทาง เช่นการ Binding ค่า input: 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
   | <template>   <div>     <input v-model="message" />     <p>{{ message }}</p>   </div> </template>
  <script> export default {   data() {     return {       message: "Hello Vue.js",     };   }, }; </script>
   | 
 
การ Binding ทำให้ข้อมูลสามารถเชื่อมโยงกับ UI และอัปเดตในทั้งสองทิศทาง ทำให้ Vue.js เป็นเครื่องมือที่สมบูรณ์และที่ดีในการจัดการและแสดงข้อมูลในหน้าเว็บแอปพลิเคชันของคุณ.