<div v-bind:class="{ active: isActive}"></div>
data: {
isActive: true
}
위 구문은 active 클래스는 isActive의 true, false에 에 의해 결정됨
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>뷰 클래스와 바인딩</title>
<style>
.red {
color: red;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<div :class="{red: isRed }">Hello</div>
<button @click="update">Click</button>
</div>
<script>
new Vue({
el: "#app",
data: {
isRed: false
},
methods: {
update() {
this.isRed = !this.isRed;
}
}
})
</script>
</body>
data를 이용하여 인라인 스타일을 적용
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>뷰 클래스와 바인딩</title>
<style>
.red {
color: red;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<div :style="styleObject">Hello</div>
</div>
<script>
new Vue({
el: "#app",
data: {
styleObject: {
color: 'red',
fontSize: '13px'
}
}
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>뷰 클래스와 바인딩</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<div :style="computedStyleObject">Hello</div>
<button @click="update()">Click</button>
</div>
<script>
new Vue({
el: "#app",
data: {
styleObject: {
red: 'red',
size: '30px'
}
},
computed: {
computedStyleObject() {
return {
color: this.styleObject.red,
size: this.styleObject.size
};
}
},
methods: {
update() {
this.styleObject.red = 'blue';
this.styleObject.size = '50px';
}
}
})
</script>
</body>
</html>