2022-06-03 14:01:46 +00:00
|
|
|
import { DefineComponent, defineComponent, h, inject, provide, Suspense, Transition } from 'vue'
|
|
|
|
import { RouteLocationNormalized, RouteLocationNormalizedLoaded, RouterView } from 'vue-router'
|
2022-01-25 14:32:09 +00:00
|
|
|
|
2022-03-14 10:47:24 +00:00
|
|
|
import { generateRouteKey, RouterViewSlotProps, wrapInKeepAlive } from './utils'
|
2022-02-07 11:32:04 +00:00
|
|
|
import { useNuxtApp } from '#app'
|
2022-03-14 10:47:24 +00:00
|
|
|
import { _wrapIf } from '#app/components/utils'
|
2022-01-25 14:32:09 +00:00
|
|
|
|
2022-04-19 08:38:57 +00:00
|
|
|
const isNestedKey = Symbol('isNested')
|
|
|
|
|
2022-01-25 14:32:09 +00:00
|
|
|
export default defineComponent({
|
|
|
|
name: 'NuxtPage',
|
2022-06-03 14:01:46 +00:00
|
|
|
inheritAttrs: false,
|
2022-02-07 11:32:04 +00:00
|
|
|
props: {
|
2022-06-03 14:01:46 +00:00
|
|
|
name: {
|
|
|
|
type: String
|
|
|
|
},
|
|
|
|
route: {
|
|
|
|
type: Object as () => RouteLocationNormalized
|
|
|
|
},
|
2022-02-07 11:32:04 +00:00
|
|
|
pageKey: {
|
|
|
|
type: [Function, String] as unknown as () => string | ((route: RouteLocationNormalizedLoaded) => string),
|
|
|
|
default: null
|
|
|
|
}
|
|
|
|
},
|
2022-06-03 14:01:46 +00:00
|
|
|
setup (props, { attrs }) {
|
2022-01-25 14:32:09 +00:00
|
|
|
const nuxtApp = useNuxtApp()
|
|
|
|
|
2022-04-19 08:38:57 +00:00
|
|
|
const isNested = inject(isNestedKey, false)
|
|
|
|
provide(isNestedKey, true)
|
|
|
|
|
2022-01-25 14:32:09 +00:00
|
|
|
return () => {
|
2022-06-03 14:01:46 +00:00
|
|
|
return h(RouterView, { name: props.name, route: props.route, ...attrs }, {
|
2022-02-07 11:32:04 +00:00
|
|
|
default: (routeProps: RouterViewSlotProps) => routeProps.Component &&
|
2022-03-14 10:47:24 +00:00
|
|
|
_wrapIf(Transition, routeProps.route.meta.pageTransition ?? defaultPageTransition,
|
2022-04-19 08:38:57 +00:00
|
|
|
wrapInKeepAlive(routeProps.route.meta.keepalive,
|
2022-04-29 09:37:49 +00:00
|
|
|
isNested && nuxtApp.isHydrating
|
2022-04-19 08:38:57 +00:00
|
|
|
// Include route children in parent suspense
|
|
|
|
? h(routeProps.Component, { key: generateRouteKey(props.pageKey, routeProps) } as {})
|
|
|
|
: h(Suspense, {
|
|
|
|
onPending: () => nuxtApp.callHook('page:start', routeProps.Component),
|
|
|
|
onResolve: () => nuxtApp.callHook('page:finish', routeProps.Component)
|
|
|
|
}, { default: () => h(routeProps.Component, { key: generateRouteKey(props.pageKey, routeProps) } as {}) })
|
|
|
|
)).default()
|
2022-01-25 14:32:09 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-06-03 14:01:46 +00:00
|
|
|
}) as DefineComponent<{
|
|
|
|
name?: string,
|
|
|
|
route?: RouteLocationNormalized
|
|
|
|
pageKey?: string | ((route: RouteLocationNormalizedLoaded) => string)
|
|
|
|
[key: string]: any
|
|
|
|
}>
|
2022-01-25 14:32:09 +00:00
|
|
|
|
|
|
|
const defaultPageTransition = { name: 'page', mode: 'out-in' }
|