본문 바로가기
Front-end/Vue

Vue 기초: style 동적 바인드

by somlang_bba 2022. 2. 14.
728x90

App.vue의 내용 

<template>
  <div>
    <h1>Hello Vue {{ username }}!!</h1>
    <h2 v-bind:class="textDecoration" class="text-red">line-through</h2>
    <h2 :class="isDone ? 'line-through' : 'highlight'">line-through</h2>
    <h2 :class="{ highlight: isDone, 'text-red': username === 'kj' }">
      Object형태의 동적 클래스
    </h2>
    <h2
      :class="[
        isDone ? 'line-through' : 'hightlight',
        username === 'kj' ? 'text-red' : '',
      ]"
    >
      Array 형태의 동적 클래스
    </h2>
  </div>
</template>

<script>
export default {
  name: "App",
  data() {
    return {
      username: "kj",
      isDone: true,
      textDecoration: "line-through",
    };
  },
};
</script>

<style>
.highlight {
  font-weight: bold;
  background: pink;
}
.text-red {
  color: red;
}
.line-through {
  text-decoration: line-through;
}
</style>

 

기본적으로 v-bind를  이용해서 script에 명시해둔 프로퍼티를 바인딩 한다.

 

v-bind:class="바인딩할 프로퍼티" 의 형식

 

v-bind는 생략 가능

 

class="삼항연산자"  class="객체" class="배열" 의 형태로 작성도 가능하다.

728x90

'Front-end > Vue' 카테고리의 다른 글

Vue3 기초: 조건부 리스트 렌더링  (0) 2022.02.21
Vue3 기초: 리스트 렌더링  (0) 2022.02.16
Vue3 기초: 조건부 렌더링  (0) 2022.02.14
Vue3 기초 : v-once  (0) 2022.02.14
Vue3 기초 : html의 attribute binding  (0) 2022.02.14