λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
[개발] Practice/Vue.js

[Vue.js] Vuex4: mapState μ‚¬μš©ν•˜κΈ°

by Connecting-the-dots 2022. 4. 8.
728x90
λ°˜μ‘ν˜•

πŸ’‘ μ‹€μŠ΅ 포인트!

πŸ’œ computed μ•Œμ•„λ³΄κΈ°

methods : {
  now(){
    return new Date()
  }
},
computed : {
  now2(){
    return new Date()
  }
},
  • ν•¨μˆ˜λ₯Ό λ§Œλ“€ λ•Œ 보톡은 methods: { } μ•ˆμ— λ§Œλ“€μ§€λ§Œ computed: { } μ•ˆμ— λ§Œλ“œλŠ” 것도 κ°€λŠ₯ν•˜λ‹€.
  • λ˜‘κ°™μ΄ λ‚ μ§œλ₯Ό μ•Œλ €μ£ΌλŠ” κΈ°λŠ₯의 ν•¨μˆ˜λ₯Ό μœ„μ™€ 같이 methods ν•­λͺ©κ³Ό computed ν•­λͺ©μ— 각각 λ§Œλ“€μ–΄μ„œ μ‚¬μš©ν–ˆμ„ λ•Œ 차이점은 λ‹€μŒκ³Ό κ°™λ‹€.
    • methods μ•ˆμ— λ§Œλ“  ν•¨μˆ˜λŠ” ν•¨μˆ˜λ₯Ό λΆ€λ₯Ό λ•Œλ§ˆλ‹€ ν•΄λ‹Ή ν•¨μˆ˜ μ•ˆμ˜ μ½”λ“œκ°€ μ‹€ν–‰λœλ‹€.
    • computed μ•ˆμ— λ§Œλ“  ν•¨μˆ˜λŠ” ν•¨μˆ˜λ₯Ό λΆˆλŸ¬λ„ ν•΄λ‹Ή ν•¨μˆ˜ μ•ˆμ˜ μ½”λ“œκ°€ μ‹€ν–‰λ˜μ§€ μ•ŠλŠ”λ‹€.
      computed: { } μ•ˆμ— μž‘μ„±ν•œ ν•¨μˆ˜λŠ” κ·Έλƒ₯ Component λ₯Ό λ‘œλ“œν•  λ•Œ λ”± ν•œλ²ˆλ§Œ μ‹€ν–‰λ˜κ³ , κ·Έ 값을 계속 μ €μž₯ν•΄μ„œ μ‚¬μš©ν•œλ‹€. 즉, μΌμ’…μ˜ 계산결과 μ €μž₯곡간이닀.
  • 그리고 computed λ‚΄μ˜ ν•¨μˆ˜λŠ” return 을 κΌ­ μ¨μ•Όν•˜λ©°, ν•¨μˆ˜λ₯Ό κ°€μ Έλ‹€ μ‚¬μš©ν•  λ•Œμ—λŠ” μ†Œκ΄„ν˜Έ 없이 ν•¨μˆ˜λͺ…λ§Œ μž‘μ„±ν•˜λ©΄ λœλ‹€.

πŸ’œ computed 둜 state μ½”λ“œ μΆ•μ•½ν•˜κΈ°

computed : {
  name(){
    return this.$store.state.name
  }
}
  • vuex λ₯Ό μ‚¬μš©ν•˜λ©΄ 데이터λ₯Ό κ°€μ Έμ˜¬ λ•Œλ§ˆλ‹€ $store.state.name~~ κ³Ό 같이 μ½”λ“œλ₯Ό μž‘μ„±ν•˜κΈ°μ—λŠ” μ½”λ“œκ°€ λ„ˆλ¬΄ κΈΈκ³  λ³΅μž‘ν•˜λ‹€. 
  • λ”°λΌμ„œ, 자주 μ‚¬μš©ν•˜λŠ” state λŠ” computed ν•­λͺ© μ•ˆμ— μΆ”κ°€ν•΄μ£Όλ©΄ μ‘°κΈˆμ΄λΌλ„ 짧게 μ‚¬μš©ν•˜λŠ” 것이 κ°€λŠ₯ν•˜λ‹€.
    (methods ν•­λͺ© μ•ˆμ— 넣어도 λ˜μ§€λ§Œ computed κ°€ 더 μ μ ˆν•΄λ³΄μ΄λ―€λ‘œ computed 에 λ„£μž!)
  • 참고둜 <script></script> μ•ˆμ—μ„œ $store λ₯Ό μ‚¬μš©ν•˜λ €λ©΄ this.$store κ³Ό 같이 μ•žμ— this λ₯Ό λΆ™μ—¬μ£Όμ–΄μ•Ό ν•œλ‹€. 
  • 이제 μœ„μ™€ 같이 μ½”λ“œλ₯Ό μž‘μ„±ν•΄μ£Όλ©΄, {{ }} μ•ˆμ— name 만 써도 name μ΄λΌλŠ” state λ₯Ό κ°€μ Έλ‹€ μ‚¬μš©ν•  수 있게 λœλ‹€.

πŸ’œ mapState 둜 computed μ•ˆμ˜ μ½”λ“œ μΆ•μ•½ν•˜κΈ°

import {mapState} from 'vuex'

computed : {
  ...mapState(['state_이름1', 'state_이름2'])
}
  • ν•˜μ§€λ§Œ μœ„μ™€ 같이 computed λ₯Ό ν™œμš©ν•œλ‹€ν•΄λ„ state κ°€ λͺ‡μ‹­κ°œ, λͺ‡λ°±κ°œκ°€ 있으면 ν•˜λ‚˜ν•˜λ‚˜ state μΆ•μ•½ μ½”λ“œλ₯Ό λ§Œλ“€κΈ°κ°€ νž˜λ“€λ‹€.
  • 이 λ•Œ, mapState λ₯Ό μ‚¬μš©ν•˜λ©΄ 보닀 νŽΈν•˜κ²Œ computed 에닀가 state λ₯Ό λ“±λ‘ν•˜λŠ” 것이 κ°€λŠ₯해진닀.
  • λ‹Ήμ—°νžˆ mapState λ₯Ό μ‚¬μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ” mapState λ₯Ό import 해와야 ν•œλ‹€.

 

import {mapState} from 'vuex'

computed : {
  ...mapState({ state_κ°€μ Έμ˜¬_λ•Œ_μ‚¬μš©ν• _이름1 : 'state_이름1'})
}
  • mapState 에 object μžλ£Œν˜•μ„ λ„£μœΌλ©΄ state λ₯Ό κ°€μ Έμ˜¬ λ•Œ μ‚¬μš©ν•  이름을 λ”°λ‘œ 지정해쀄 μˆ˜λ„ μžˆλ‹€.

 

import {mapState, mapMutations} from 'vuex'

computed : {
  ...mapState(['state_이름1', 'state_이름2']),
  ...mapMutations([ 'mutations_ν•¨μˆ˜_이름1', 'mutations_ν•¨μˆ˜_이름2' ])
}
  • μœ„μ™€ 같이 μ½”λ“œλ₯Ό μž‘μ„±ν•˜λ©΄ mutations λ‚΄μ˜ ν•¨μˆ˜λ„ κ°€μ Έλ‹€ μ‚¬μš©ν•˜λŠ” 것이 κ°€λŠ₯ν•˜λ‹€.
  • 예λ₯Ό λ“€μ–΄, mutations 내에 changeLikes λΌλŠ” ν•¨μˆ˜κ°€ μžˆμ„ λ•Œ, ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•  λ•Œλ§ˆλ‹€ $store.commit('changeLikes') 라고 μž‘μ„±ν•˜λ˜ 것을 changeLikes() μ΄λ ‡κ²Œλ§Œ μ½”λ“œλ₯Ό μž‘μ„±ν•΄λ„ μ‚¬μš©μ΄ κ°€λŠ₯ν•œ 것이닀.
  • 이외에도 mapActions 같은 것도 μžˆλ‹€κ³  ν•˜λ‹ˆ, ν•„μš”ν•˜λ©΄ κ΅¬κΈ€λ§ν•΄μ„œ μ‚¬μš©λ°©λ²•μ„ μ΅ν˜€ 직접 μ‚¬μš©ν•΄λ³΄μž.

 

728x90
λ°˜μ‘ν˜•