Skip to content

Commit 5deb552

Browse files
committed
add 《Vuex状态管理的工作原理》
1 parent 1ee0c31 commit 5deb552

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
let Vue;
2+
3+
class Store {
4+
constructor () {
5+
this._vm = new Vue({
6+
data: {
7+
$$state: this.state
8+
}
9+
})
10+
}
11+
12+
commit (type, payload, _options) {
13+
const entry = this._mutations[type];
14+
entry.forEach(function commitIterator (handler) {
15+
handler(payload);
16+
});
17+
}
18+
19+
dispatch (type, payload) {
20+
const entry = this._actions[type];
21+
22+
return entry.length > 1
23+
? Promise.all(entry.map(handler => handler(payload)))
24+
: entry[0](payload);
25+
}
26+
}
27+
28+
function vuexInit () {
29+
const options = this.$options;
30+
if (options.store) {
31+
this.$store = options.store;
32+
} else {
33+
this.$store = options.parent.$store;
34+
}
35+
}
36+
37+
export default install (_Vue) {
38+
Vue.mixin({ beforeCreate: vuexInit });
39+
Vue = _Vue;
40+
}

0 commit comments

Comments
 (0)