This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
page.ts
54 lines (48 loc) · 1.8 KB
/
page.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { defineComponent, h, Suspense, Transition, nextTick } from 'vue'
import { RouteLocationNormalizedLoaded, RouterView } from 'vue-router'
import { generateRouteKey, RouterViewSlotProps, wrapInKeepAlive } from './utils'
import { useNuxtApp } from '#app'
import { _wrapIf } from '#app/components/utils'
export default defineComponent({
name: 'NuxtPage',
props: {
pageKey: {
type: [Function, String] as unknown as () => string | ((route: RouteLocationNormalizedLoaded) => string),
default: null
}
},
setup (props) {
const nuxtApp = useNuxtApp()
function getTransitionProps (routeProps: RouterViewSlotProps) {
const metaTransition = routeProps.route.meta.pageTransition
const onAfterLeave = () => {
nuxtApp.callHook('page:transition:finish', routeProps.Component)
}
if (typeof metaTransition === 'boolean') {
return metaTransition && {
...defaultPageTransition,
onAfterLeave
}
} else {
return {
...defaultPageTransition,
...(metaTransition || {}),
onAfterLeave
}
}
}
return () => {
return h(RouterView, {}, {
default: (routeProps: RouterViewSlotProps) => routeProps.Component &&
_wrapIf(Transition, getTransitionProps(routeProps),
wrapInKeepAlive(routeProps.route.meta.keepalive, h(Suspense, {
onPending: () => nuxtApp.callHook('page:start', routeProps.Component),
onResolve: () => {
nextTick(() => nuxtApp.callHook('page:finish', routeProps.Component))
}
}, { default: () => h(routeProps.Component, { key: generateRouteKey(props.pageKey, routeProps) } as {}) }))).default()
})
}
}
})
const defaultPageTransition = { name: 'page', mode: 'out-in' }