ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • watch
    [공부] 프로그래밍/Vue.js 2023. 10. 22. 14:43

     

    watch

     

    computed와 다른점은 코드가 길어지고, return이 필요없다는 점

    • immediate : 초기값 표시여부 설정

     

    // HTML
    <template>
    	
      <input type="number" v-model="price1" />
      <br>
      <input type="number" v-model="price2" />
      <br>
      <input type="number" v-model="price3" />
      <br>
    
      <span>合計:{{ calc1 }}</span>
      <br>
      <span>合計に対する10%:{{ calc2 }}</span>
      
    </template>
    
    // Javascript
    <script setup>
      import { ref, watch } from 'vue'
    	
      const price1 = ref(1000);
      const price2 = ref(1000);
      const price3 = ref(1000);
    
      let calc1 = ref(0);
      watch(
        () => price1.value + price2.value + price3.value,
        (newValue, oldValue) => {
          calc1.value = newValue;
        },
        { immediate: true }
      );
    
      let calc2 = ref(0);
      watch(
        () => (price1.value + price2.value + price3.value) * 0.1,
        (newValue, oldValue) => {
          calc2.value = newValue;
        },
        { immediate: true }
      );
    
      
    </script>

     

    '[공부] 프로그래밍 > Vue.js' 카테고리의 다른 글

    if 구문  (0) 2023.10.22
    이벤트 설정  (0) 2023.10.22
    ref 데이터의 연동 3  (0) 2023.10.16
    computed  (0) 2023.10.16
    ref 데이터의 연동 2  (0) 2023.10.16
Designed by Tistory.