๐Ÿ“• 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>