kVue.js
let Vue
class Store {
constructor (options) {
// this.$store.state.count 这样使用的
this.state = new Vue({
data: options.state
})
this.mutations = options.mutations
this.actions = options.actions
}
commit=(type, args) => {
this.mutations[type](this.state, args)
}
dispatch (type, args) {
this.actions[type]({
commit: this.commit, state: this.state
}, args)
}
}
function install (_Vue) {
Vue = _Vue
Vue.mixin({
beforeCreate () {
if (this.$options.store) {
Vue.prototype.$store = this.$options.store
}
}
})
}
export default { Store, install }
index.js
import Vue from 'vue'
// import Vuex from 'vuex'
import Vuex from './kVuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
count: 1
},
mutations: {
increment (state, n = 1) {
state.count += n
}
},
actions: {
incrementAction ({ commit }, { n }) {
commit('increment', n)
}
},
modules: {
}
})