๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“  archive

[Vue.js ์ƒํƒœ๊ด€๋ฆฌ 1ํŽธ] Vue.Observable ์„ ํ†ตํ•œ ์ƒํƒœ๊ด€๋ฆฌ

by HandHand 2022. 3. 7.

๐Ÿ“Œ Vue.Observable?

Vue 2.6 ๋ฒ„์ „๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜์‘ํ˜•์œผ๋กœ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค.

์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” ์ด๋ฅผ ํ™œ์šฉํ•œ ๊ฐ„๋‹จํ•œ ์ƒํƒœ ๊ด€๋ฆฌ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“Œ Store ํŒจํ„ด ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

๊ฐ„๋‹จํ•œ counter ์•ฑ์„ ํ†ตํ•ด์„œ Vue.observable ์„ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋จผ์ € ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด count ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด count ๊ฐ€ 1์”ฉ ์ฆ๊ฐ€ํ•˜๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

<template>
  <div>
    {{ count }}
    <button @click="onClick">ํด๋ฆญ</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      count: 0,
    }
  },

  methods: {
    onClick() {
      this.count += 1
    },
  },
}
</script>

 

ํ˜„์žฌ ์ปดํฌ๋„ŒํŠธ ์ƒํƒœ ๊ฐ’์œผ๋กœ ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๋Š” count ๊ฐ’์„ ์ „์—ญ์—์„œ ๊ด€๋ฆฌํ•˜๋„๋ก ๋ณ€๊ฒฝํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋จผ์ € store ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  index.js ํŒŒ์ผ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

import Vue from "vue"

export const mapState = (state, properties = []) => {
  const computed = {}
  properties.forEach((prop) => (computed[prop] = () => state[prop]))
  return computed
}

export const state = Vue.observable({
  count: 0,
})

export const mutations = {
  increaseCount() {
    state.count += 1
  },
}

mapState ๋Š” store ์— ์ •์˜๋˜์–ด์žˆ๋Š” ์ƒํƒœ ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์˜

computed ์†์„ฑ์— ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งคํ•‘ํ•ด์ฃผ๋Š” ํ—ฌํผ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

์ด๋Š” store ์˜ state ์— ๋Œ€ํ•œ ์ง์ ‘์ ์ธ ์ ‘๊ทผ ๋ฐ ์ˆ˜์ •์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

 

state ๋Š” Vue.observable ์„ ํ†ตํ•ด ๋ฐ˜ํ™˜๋œ ๋ฐ˜์‘ํ˜• ๊ฐ์ฒด์ด๋ฉฐ

์ด๋ฅผ ์กฐ์ž‘ํ•˜๋Š” mutations ๋„ ํ•จ๊ป˜ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค.

(๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ actions ๋‚˜ getters ๋„ ์ •์˜ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜„.)

 

์ด์ œ ๊ธฐ์กด์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.

count ๋ฅผ ์ „์—ญ ์ƒํƒœ๋กœ ์˜ฎ๊ฒผ์„ ๋ฟ, ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<template>
  <div>
    {{ count }}
    <button @click="onClick">ํด๋ฆญ</button>
  </div>
</template>

<script>
import { state, mapState, mutations, } from "../store"

export default {
  computed: {
    ...mapState(state, ["count"]),
  },

  methods: {
    onClick() {
      mutations.increaseCount()
    },
  },
}
</script>

 

๐Ÿ“Œ ์–ธ์ œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„๊นŒ?

๊ทธ๋ ‡๋‹ค๋ฉด ์–ด๋–ค ๊ฒฝ์šฐ์— Vue.observable ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ƒํƒœ ๊ด€๋ฆฌ๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„๊นŒ์š”?

๊ทœ๋ชจ๊ฐ€ ํฌ๊ฑฐ๋‚˜ ํ™•์žฅ๋  ๊ฒƒ ๊ฐ™์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋Š” Vuex ๋‚˜ Pinia ์™€ ๊ฐ™์€ ์ƒํƒœ ๊ด€๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ

์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด๋“ค์€ ๋Œ€๊ทœ๋ชจ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์ ํ•ฉํ•œ ๋ชจ๋“ˆํ™”์™€ ์—ฌ๋Ÿฌ ๊ธฐ๋Šฅ๋“ค์„ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๊ทธ๋ ‡์ง€๋งŒ ์ž‘์€ ๊ทœ๋ชจ์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋Š” ๊ตณ์ด Vuex ์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•ด์„œ ์ƒํƒœ ๊ด€๋ฆฌ๋ฅผ ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค.

์ด๋Š” boilerplate ์ฝ”๋“œ๋กœ ์ธํ•ด ์˜คํžˆ๋ ค ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ฝ”๋“œ ๋ณต์žก๋„๋ฅผ ๋†’์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ์˜ ํ๋ฆ„์ด ๊ฐ„๋‹จํ•˜๊ฑฐ๋‚˜, ๊ทœ๋ชจ๊ฐ€ ์ž‘์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋Š”

Vue.observable ๋กœ๋„ ์ถฉ๋ถ„ํžˆ ์ƒํƒœ ๊ด€๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•  ๊ฒƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ“Œ ์ฐธ๊ณ  ์ž๋ฃŒ

Home Rolled Store with Vue.observable (Vue 2) - Vue.js Tutorials

Deep dive into Vue state management - Vue.js Tutorials

API - Vue.js

๋ฐ˜์‘ํ˜•

๐Ÿ’ฌ ๋Œ“๊ธ€