OKdevTV Docs
Home OKdevTV
ESC
vue 1 min read 0.9 KB Raw Markdown
On this page (3)

Vuex

Install

text
npm install vuex@next --save

Concept

  • state: Shared data itself
  • getters: Read gateway of state
  • actions: Asynchronous changing data
  • mutations: Synchronous changing data
  • modules: Fractally Scaled out group of states

Simple Example

js
import { createApp } from 'vue'
import { createStore } from 'vuex'

// Create a new store instance.
const store = createStore({
  state () {
    return {
      count: 0
    }
  },
  mutations: {
    increment (state) {
      state.count++
    }
  }
})

const app = createApp({ /* your root component */ })

// Install the store instance as a plugin
app.use(store)
js
store.commit('increment')
console.log(store.state.count) // -> 1
js
methods: {
  increment() {
    this.$store.commit('increment')
    console.log(this.$store.state.count)
  }
}