반응형
<template>
  <div>
    <h1>Computed</h1>
    <h2>{{ address }}</h2>
    <h2>내가 받은 점수의 합 {{ totalScore }}</h2>
    <h2>method : {{ getTotalScore() }}</h2>
    영어점수<input type="number" v-model="grade.eng" /> 학생이름<input
      type="text"
      v-model="studentName"
    />
  </div>
</template>
<script>
export default {
  name: "App",
  data() {
    return {
      username: "scalper",
      address1: "성남시",
      address2: "분당구",
      address3: "정자로",
      grade: {
        math: 70,
        kor: 90,
        eng: 50,
        sci: 55,
      },
      studentName: "",
    };
  },
  computed: {
    //computed는 cache가 된다 !!
    address() {
      return `${this.address1} - ${this.address2} - ${this.address3}`;
    },
    totalScore() {
      console.log("computed!!");
      // destructuring
      const { math, kor, eng, sci } = this.grade;
      return math + kor + eng + sci;
    },
  },
  methods: {
    //메서드는 화면에 변동이 있을 때마다 계산을 한다!
    getTotalScore() {
      console.log("methods!!");
      const { math, kor, eng, sci } = this.grade;
      return math + kor + eng + sci;
    },
  },
};
</script>

<style>
a {
  font-size: 24px;
  display: block;
}

label {
  font-size: 22px;
  font-weight: bold;
  margin-right: 1 rem;
}

div {
  margin-bottom: 1rem;
}
</style>

Computed는 간략한 계산

값 캐시효과

 

'Web > Vue' 카테고리의 다른 글

[Vue] Component 1  (0) 2022.06.25
[Vue] Watch  (0) 2022.06.25
[Vue] Directives  (0) 2022.06.24
[Vue] 폼 핸들링  (0) 2022.06.24
[Vue] 이벤트  (0) 2022.06.24

+ Recent posts