-
-
Save carloslema/9ef7d2552aab238b164e619fdd4a3687 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