-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathComponentWithData.vue
41 lines (35 loc) · 1.02 KB
/
ComponentWithData.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
<template>
<div>
<p>Here is the data: {{ fromApi }}</p>
other {{ other }}
</div>
</template>
<script lang="ts">
import { defineComponent, toRefs, reactive } from 'vue'
import { getData, delay } from '../api'
import { onBeforeRouteUpdate } from 'vue-router'
const ComponentWithData = defineComponent({
name: 'ComponentWithData',
async setup() {
const data = reactive<{ other: string, fromApi: null | {message: string, time: number }}>({ other: 'old', fromApi: null })
onBeforeRouteUpdate(async (to, from, next) => {
data.fromApi = await getData()
next()
})
data.fromApi = await getData()
return {
...toRefs(data),
}
},
async beforeRouteEnter(to, from, next) {
console.log('this in beforeRouteEnter', this)
await delay(300)
next(vm => {
console.log('got vm', vm);
// Workaround for https://github.com/vuejs/router/issues/701
(vm as InstanceType<typeof ComponentWithData>).other = 'Hola'
})
},
})
export default ComponentWithData
</script>