Created
January 25, 2018 21:29
-
-
Save shinshin86/5db25b3ae1d8ff3a7539b0a9dd1f6ff4 to your computer and use it in GitHub Desktop.
vuex plugin sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Thanks for this :)