-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.vue
48 lines (42 loc) · 1.36 KB
/
component.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<template>
<div>
<slot :state="state" :action="stores.actions" :business="stores.business"></slot>
</div>
</template>
<script>
export default {
props: ['stores'],
data() {
return {
state: this.stores && this.stores.store ? this.stores.store.getState() : {},
};
},
methods: {
onStateChange() {
this.state = this.stores.store.getState();
},
},
mounted () {
if (this.stores && this.stores.store && this.stores.store.subscribe) {
this.unsubscribe = this.stores.store.subscribe(this.onStateChange.bind(this));
this.onStateChange.call(this);
}
},
beforeDestroy () {
this.unsubscribe && this.unsubscribe();
this.unsubscribe = undefined;
},
watch: {
stores(val, oldVal) {
if (val !== oldVal) {
this.unsubscribe && this.unsubscribe();
this.unsubscribe = undefined;
if (val && val.store && val.store.subscribe) {
this.unsubscribe = val.store.subscribe(this.onStateChange.bind(this));
this.onStateChange.call(this);
}
}
},
},
};
</script>