TIL | Vuex

김윤희·2022년 9월 15일
0

✔ Vuex란?

React에서 리덕스에 해당하는 걸로 전역 상태를 관리해준다 (단방향)

📍 Vuex 사용방법은?

  • npm install vuex --save

  • main.js

import Vue from 'vue'
import App from './App.vue'
import router from './router'
import Vuex from 'vuex'

Vue.config.productionTip = false

Vue.use(Vuex);

const store = new Vuex.Store({
  state: {
    count:0
  },
  mutations:{
    increment(state){ // 어떤 state에 값을 변화시킬때 
      state.count++
    }
  }
})

store.commit('increment') // mutations 안에 increment 호출

console.log(store.state.count) // 1

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

0개의 댓글