๐Ÿ’ป VueJS Computed & Watch

waterglassesยท2022๋…„ 5์›” 8์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
22/50
post-thumbnail

์ด๋ฒˆ ์ฑ•ํ„ฐ๋Š” Vuejs์˜ computed์™€ watch์— ๋Œ€ํ•ด ๋‹ค๋ฃจ์—ˆ์Šต๋‹ˆ๋‹ค.
โš ๏ธ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์€ ์˜คํƒ€๋‚˜ ์ž˜๋ชป๋œ ์ •๋ณด๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ“๊ธ€๋กœ ์•Œ๋ ค์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๐Ÿ“ƒ ์˜ค๋Š˜ ๊ณต๋ถ€ํ•œ ๊ฒƒ

Computed

ํ…œํ”Œ๋ฆฟ ์•ˆ์—์„œ ๋งŽ์€ ์—ฐ์‚ฐ์„ ํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ์ปค์ง€๊ณ  ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์–ด๋ ต๊ธฐ ๋•Œ๋ฌธ์— ์ข‹์€ ๋ฐฉํ–ฅ์ด ์•„๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด computed์ด๋‹ค. ๋˜ํ•œ computed์˜ ๊ฐ’์€ ์บ์‹ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ฆฌ๋ Œ๋”๋ง ๋์„ ๋•Œ, ๊ฐ™์€ ๊ฐ’์ด ๋“ค์–ด์™”๋‹ค๋ฉด ์—ฐ์‚ฐํ•˜์ง€ ์•Š๋Š”๋‹ค.

<template>
  <div>
    <p>{{ increse }}</p>
  </div>
</template>
<script>
export default {
  data() {
    return {
      score: 1
    };
  },
  computed: {
    increse() {
      return score + 1;
      //data์— ์ง์ ‘ ์ ‘๊ทผํ•˜์—ฌ ๊ณ„์‚ฐํ•œ ๊ฒฐ๊ณผ๋ฅผ ํ…œํ”Œ๋ฆฟ์— ๋ณด์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    }
  }
};
</script>

์œ„์™€ ๊ฐ™์ด computed ์†์„ฑ์€ ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ์ด ๊ฐ€๋Šฅํ•˜๊ณ  data๊ฐ€ ๋ฐ”๋€œ์— ๋”ฐ๋ผ computed ๊ฐ’๋„ ์ž๋™์œผ๋กœ ๋ฐ”๋€Œ์–ด ๋ Œ๋”๋ง ๋œ๋‹ค.

computed getter/setter

<template>
  <div id="demo">{{ fullName }}</div>
</template>
<script>
export default {
  computed: {
    fullName: {
      // getter
      get: function() {
        return this.firstName + " " + this.lastName;
      },
      // setter
      set: function(newValue) {
        [this.firstName, this.lastName] = newValue.split(" ");        
      }
    }
  }
};
</script>

์œ„์ฒ˜๋Ÿผ fullName์ด ๋ณ€๊ฒฝ๋˜๋ฉด set์ด ์‹คํ–‰๋˜๊ณ  firstName๊ณผ lastName์ด ๋™์‹œ์— ๋ฐ”๋€Œ๊ฒŒ ๋œ๋‹ค.

Watch

๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ computed ์†์„ฑ์ด ๋” ์ ˆ์ ํ•˜์ง€๋งŒ, watch๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค. ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์— ๋Œ€ํ•œ ์‘๋‹ต์œผ๋กœ ๋น„๋™๊ธฐ ํ˜น์€ ๋น„์šฉ์ด ๋งŽ์ด ๋“œ๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋Š” ๊ฒฝ์šฐ์— ์œ ์šฉํ•˜๋‹ค. watch๋Š” ๊ฐ™์€ ๊ฐ’์ด์–ด๋„ ์—ฐ์‚ฐ์„ ๋‹ค์‹œํ•œ๋‹ค๋Š” ํŠน์ง•๋„ ์žˆ๋‹ค.

fruits๋ฅผ ๊ฐ์‹œํ•˜๋ฉด์„œ ์˜ ๊ฐ’์ด ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค handler()ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๋Š” ์˜ˆ์ œ์ด๋‹ค.

<template>
  <div id="app">
    <button @click="capitalize">capitalize</button>
    <ul>
      <li v-for="fruit in fruits">{{ fruit.name }}</li>
    </ul>
  </div>
</template>

const App = {
  data() {
    return {
      fruits: [
        { id: 1, name: 'Apple' },
        { id: 2, name: 'Banana' },
        { id: 3, name: 'Cherry' }
      ]
    }
  },
  watch: {
    fruits: {
      handler() { 
        console.log(this.fruits)
      },
      deep: true, // ์ค‘์ฒฉ ๋ฐ์ดํ„ฐ๋„ ๊ฐ์‹œ
      immediate: true // ๋ฐ์ดํ„ฐ ๋ณ€๊ฒฝ์‹œ ์‹คํ–‰ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฐ์ดํ„ฐ๊ฐ€ ์ค€๋น„๊ฐ€ ๋˜์ž๋งˆ์ž ์ฆ‰์‹œ ์‹คํ–‰
    }
  },
  methods: {
    capitalize() {
      this.fruits.forEach(fruit => {
        fruit.name = fruit.name.toUpperCase()
      })
    }
  }
}
const vm = Vue.createApp(App).mount('#app')

๐Ÿ”ฅ ๋Š๋‚€์ 

computed์™€ watch์— ๋Œ€ํ•ด์„œ ๋ฐฐ์› ๋Š”๋ฐ ์ฐจ์ด์ ์— ๋Œ€ํ•ด์„œ๋„ ์•Œ๊ฒŒ ๋˜์—ˆ๊ณ  ํ™•์‹คํžˆ ์‹ค์Šต๊ณผ ํ•จ๊ป˜ ์ง„ํ–‰ํ•˜๋‹ค ๋ณด๋‹ˆ ์ดํ•ด๊ฐ€ ๋” ๋นจ๋ฆฌ ๋˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๊ทธ๋™์•ˆ ๋ฐ”๋‹๋ผJS๋กœ state๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ๋ณ€๊ฒฝํ•˜๊ณ  ๊ฐ์ง€ํ•ด์•ผํ–ˆ์„ ๋•Œ์™€๋Š” ๋‹ฌ๋ฆฌ watch, computed ๋“ฑ์œผ๋กœ ๋ณ€๊ฒฝ์„ ์‰ฝ๊ฒŒ ๊ฐ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ๋งค๋ ฅ์ ์ด์—ˆ๋‹ค. ์—ญ์‹œ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ ๊ฐ€ ์žˆ๋‹ค.

vue ์žฌ๋ฏธ์žˆ๋‹ค. ๋“œ๋””์–ด ์ด๋ฒˆ์ฃผ ๋ฐ€๋ฆฐ ๊ฐ•์˜ ์ฒญ์‚ฐ ์™„๋ฃŒ

Refer

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ๋ฐ๋ธŒ์ฝ”์Šค
Vue - compted ์†์„ฑ๊ณผ watch

์ •๋ฆฌ

Day35

profile
๋งค ์ˆœ๊ฐ„ ์„ฑ์žฅํ•˜๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๋ ค๊ณ  ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€