잡다한 지식의 블로그
Vue.js에서 Class와 Style 속성 Binding 본문
Vue.js에서 Class와 Style Binding
- Class와 Style 둘 다 속성이므로 다른 속성과 마찬가지로 v-bind를 사용하여 문자열 값을 동적으로 할당할 수 있다.
- 클래스를 통적으로 이용하기 위해 객체를 v-bind:class에 전달할 수 있다.
- v-bind:class는 :class로 줄여서 사용이 가능하다.
Vue.js에서 Class와 Style Binding의 예
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Class and Style Binding</title>
<style>
.red {
color: red;
}
.bold {
font-weight: bold;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<div :class="{ red : isRed, bold : isBold }">
hello
</div>
<br>
<div :style="{ color : red, fontSize: size }">
hello
</div>
<br>
<button @click="update">click</button>
</div>
<script>
new Vue({
el: '#app',
data: {
isRed : false,
isBold : false,
red : 'red',
size : '30px'
},
methods: {
update() {
this.isRed = !this.isRed;
this.isBold = !this.isBold;
}
}
})
</script>
</body>
</html>
'Vue.js' 카테고리의 다른 글
Vue.js에서 v-for (0) | 2024.04.17 |
---|---|
Vue.js에서 조건부 렌더링 (0) | 2024.04.17 |
Vue.js Watch 속성 (0) | 2024.04.15 |
Vue.js Computed 속성 (0) | 2024.04.11 |
Vue.js 이벤트핸들링 - v-on (0) | 2024.04.08 |
Comments