Skip to content

Instantly share code, notes, and snippets.

@shinshin86
Created January 25, 2018 21:29
Show Gist options
  • Save shinshin86/5db25b3ae1d8ff3a7539b0a9dd1f6ff4 to your computer and use it in GitHub Desktop.
Save shinshin86/5db25b3ae1d8ff3a7539b0a9dd1f6ff4 to your computer and use it in GitHub Desktop.
vuex plugin sample
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const incrementPlugin = store => {
store.subscribe((mutation, state) => {
if(mutation.type === "showCount") {
state.count++
}
})
}
const store = new Vuex.Store({
state: {
count: 0,
name: ""
},
mutations: {
showCount(state) {
console.log(state.count)
},
setName(state) {
state.name = "hoge"
}
},
plugins: [incrementPlugin]
})
// babel-node vuex-plugin-sample.js
store.commit('showCount') // 0
console.log(store.state.count) // 1
store.commit('setName')
console.log(store.state.count) // 1
console.log(store.state.name) // hoge
@coeneivan
Copy link

Thanks for this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment