-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
34 lines (31 loc) · 877 Bytes
/
index.ts
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
const eventsPlugins = (Vue) => {
Vue.mixin({
beforeCreate() {
Vue.util.defineReactive(this, '$fetchState', {
pending: false,
error: null,
});
},
async created() {
let options = this.$options;
let fetchMethod = options.fetch;
if (fetchMethod && typeof fetchMethod === 'function') {
try {
this.$fetchState.pending = true;
this.$fetchState.error = null;
const result = await fetchMethod.apply(this);
if (result && typeof result === 'object') {
for (const key in result) {
Vue.util.defineReactive(this, key, result[key]);
}
}
} catch (error) {
this.$fetchState.error = error;
} finally {
this.$fetchState.pending = false;
}
}
},
});
};
export default eventsPlugins;