Skip to content

Instantly share code, notes, and snippets.

@carloslema
Forked from shinshin86/vuex-plugin-sample.js
Created August 10, 2020 18:49
Show Gist options
  • Save carloslema/9ef7d2552aab238b164e619fdd4a3687 to your computer and use it in GitHub Desktop.
Save carloslema/9ef7d2552aab238b164e619fdd4a3687 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment