๐ Language/Vue
[Age of Vue] Vue.js ์์ํ๊ธฐ - 27. computed ์์ฑ์ ์ด์ฉํ class ์ฝ๋ ์์ฑ
a n u e
2022. 5. 8. 12:41
computed ์์ฑ์ class binding์์๋ ์ฎ์ด ์ฌ์ฉํ ์ ์๋ค.
๊ตณ์ด computed๋ฅผ ์ฌ์ฉํ๊ณ , methods ์์ฑ์ ์ฌ์ฉํ์ง ์๋ ์ด์ ๊ฐ ๋ญ๊น? ... ๊ทธ๋ฅ ์์์ธ๊ฐ?

<!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>Document</title>
<style>
.warning {
color: red;
}
</style>
</head>
<body>
<div id="app">
<!-- <p v-bind:class="cname">Hello</p> -->
<p v-bind:class="errorTextColor">Hello</p>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
new Vue({
el: '#app',
data: {
// cname: 'blue-text',
isError: false
},
computed: {
errorTextColor: function() {
// if (isError) {
// return 'warning'
// } else {
// return null;
// }
return this.isError ? 'warning' : null;
}
}
});
</script>
</body>
</html>