基本框架:这是一个Vuex的组件级的解决方案
- 解决父子,兄弟组件的通信问题
- 解决Vuex的单例问题,因为Vuex是单例的,所以需要重复使用的组件不应该依赖Vuex,会造成多个实例公用一套状态
const Store = function (initialState = {}) {
this.state = {
loading: true
};
Object.assign(this.state, initialState);
}
Store.prototype.mutations = {
async loadstart(state) {
state.loading = true;
},
async loadend(state) {
state.loading = false;
}
};
Store.prototype.commit = function (name, ...args) {
const mutations = this.mutations;
if (mutations[name]) {
return mutations[name].apply(this, [this.state].concat(args));
} else {
throw new Error(`Action not found: ${name}`);
}
};
export default Store;
网友评论