VueX
Vuex is the state management pattern being used in Vue.
Vuex is one-way-data flow. see picture below
![]() |
| Vuex state flow |
As you see above image shows that vue component is apart from vuex. Vuex allows you to manage or handle state outside vue component. This is best practice for large-scale-SPA apps.
Installation
This is done with Npm
npm install vuex --save
This is done with Yarn
yarn add vuex
Vuex requires promise.
You may need to install promise as
npm install es6-promise --save
or
yarn add es6-promise
Example:
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex)
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment (state) {
state.count++
}
}
})
store.commit('increment') // result 1
store.commit('increment') // result 2
console.log(store.state.count)
Vue.use(Vuex) is very important to have this to tell vue to use vuex state management.
state: is the initial object in vuex instance.
mutations: is to change or modify any value you prefer but not yet change value of count:0.
store.commit('increment'): is to commit the change to count:1.
Final result is 2 as you commit two times.

Comments
Post a Comment