Vue.js เริ่มต้น ตอน7 (Class and Style Bindings)

2 Jun 2018,
Share: 

ในตอนที่ 7 นี้จะเกี่ยวกับเรื่อง Class and Style Bindings ใน vue.js ครับ ในการจัดการ Class และ Style เราสามารถใช้ v-bind ได้ เพื่อให้เราสามารถใส่ Logic หรือ ตัวแปรลงไปได้ ทําให้เราสามารถทํา Class  และ Style แบบ Dinamic ได้ง่ายขึ้น

เรามาดูวิธีการใช้งานในรูปแบบต่างๆ โดยผมจะแยกเป็นสองส่วนครับคือ Binding HTML Classes และ Binding Inline Styles ดังนี้ครับ

Binding HTML Classes

Object Syntax คุณสามารถส่งค่า Object ให้กับ Class ได้ตามตัวอย่างนี้

<div v-bind:class="{ active: isActive }"></div>

จากตัวอย่าง isActive เป็นตัวแปรที่เราเก็บค่า true หรือ false ไว้เพื่อ Control class ของ div นี้ได้ตามต้องการ นอกจากนี้เราสามารถใช้ v-bind:class ร่วมกับ Attribute class ปกติได้ ในกรณีที่เราต้องการควบคุม Class เพิ่มเติมนอกเหนือจาก Class เดิมที่มีอยู่ ตัวอย่าง

<div class="static"
     v-bind:class="{ active: isActive, 'text-danger': hasError }">
</div>

isActive, hasError เป็นตัวแปรที่เราสามารถควบคุมได้ตามต้องการ มาดูตัวอย่างการทําไปใช้งานจริงกันครับ

data: {
  isActive: true,
  error: null
},
computed: {
  classObject: function () {
    return {
      active: this.isActive && !this.error,
      'text-danger': this.error && this.error.type === 'fatal'
    }
  }
}

html:

<div v-bind:class="classObject"></div>

Array Syntax เราสามารถส่ง Array เข้าไปใน v-bind:class ได้ ดังนี้ js:

<div v-bind:class="classObject"></div>

html:

data: {
  activeClass: 'active',
  errorClass: 'text-danger'
}

เมื่อ Render ออกมาแล้วจะได้ออกมาแบบนี้ครับ

<div class="active text-danger"></div>

หรือ เราสามารถเพิ่ม Condition เข้าไปได้แบบนี้

<div v-bind:class="[isActive ? activeClass : '', errorClass]"></div>

With Components ในกรณีที่เราใส่ Class ให้กับ Component ที่เราสร้างขึ้นมาเอง Class ที่เรากําหนดนั้นจะถูกกําหนดให้กับ Class ของ Root ด้วย ตัวอย่าง ถ้าเรามี Component ชื่อ my-component โดยมีเนื้อหาดังนี้

<template>
  <p class="foo bar">Hi</p>
</template>

<script>
export default {
  name: 'my-component'
}
</script>

แล้วเรานํา Component ไปใช้งานโดยกําหนด Class ให้กับ Component นั้นด้วย

<my-component class="baz boo"></my-component>

เมื่อ Render ออกมาแล้วจะได้แบบนี้ครับ

<p class="foo bar baz boo">Hi</p>

Binding Inline Styles

Object Syntax เราสามารถส่ง Styles ให้กับ HTML ได้ซึ่งก็จะ คล้ายๆกับ Class โดยใช้ v-bind:style แล้วส่ง Object ของ style ที่เราต้องการเข้าไป ตัวอย่าง

<div v-bind:style="{ color: activeColor, fontSize: fontSize + 'px' }"></div>

ตัวอย่าง data:

data: {
  activeColor: 'red',
  fontSize: 30
}

หรือเราสามารถทําแบบนี้ได้ครับ

<div v-bind:style="styleObject"></div>

สร้าง Data ที่เป็น Object ก็ได้ครับ

data: {
  styleObject: {
    color: 'red',
    fontSize: '13px'
  }
}

Array Syntax ถ้าต้องการใช้ Style หลาย Style ก็สามารถส่งเป็น Array เข้าไปได้ดังนี้

<div v-bind:style="[baseStyles, overridingStyles]"></div>

Auto-prefixing กรณีที่ Style ต้องการใช้  vendor prefixes ใน v-bind:style Vue จะตรวจสอบและ เพิ่มให้โดยอัตโนมัติ ถึงจุดนี้เราสามารถควบคุม Class และ Style ในแบบ Dinamic ได้อย่างง่ายดาย ด้วยการใช้ v-bind:class และ v-bind:style แล้วพบกันบทความหน้านะครับ สวัสดีครับ :)

Suggestion blogs

Vue.js เริ่มต้น ตอน5 (ทําความรู้จักกับ Props)

หลังจากที่เราเรียนรู้ Component กันไปแล้ว ถ้ายังจํากันได้ภายใน Component มี Option ตัวนึงชื่อว่า Props ซึ่งมีหน้าที่ รับข้อมูลจากภายนอก Component เข้ามาใช้งานภายใน Component โดยจะรับข้อมูลผ่านทาง Attribute การใช้งาน Props สามารถใช้งานได้หลายรูปแบบ เราจะมาเรียนรู้ไปทีละแบบนะครับ

ค่าความต้านทานมาตรฐานที่มีจําหน่ายตามท้องตลาด

ในการออกแบบวงจรอิเล็กทรอนิกส์ ค่าความต้านทานที่คํานวณได้อาจจะไม่มีขายตามท้องตลาด ดังนั้นเราจึงต้องใช้ค่าที่ใกล้เคียงหรือ นํามาต่อ ขนาน-อนุกรม กันเพื่อให้ได้ค่าที่ใกล้เคียงที่สุด ข้อมูลในตารางเป็นค่าความต้านทานที่มี

Docker Container

สําหรับบทความนี้จะมีเนื้อหาเกี่ยวกับ Docker Container นะครับ เราจะมาดู และทําความเข้าใจกันว่า Docker Container มันคืออะไร และเกี่ยวข้องกับ Docker Image อย่างไร


Copyright © 2019 - 2025 thiti.dev |  v1.54.0 |  Privacy policy | 

Build with ❤️ and Astro.

Github profile   Linkedin profile   Instagram   X profile   Nostr   Youtube channel   Telegram   Email contact   วงแหวนเว็บ