2020-12-04 21:25:33 +08:00
import type { FunctionalComponent } from 'vue' ;
import { computed , defineComponent , unref , Transition , KeepAlive } from 'vue' ;
import { RouterView , RouteLocation } from 'vue-router' ;
import FrameLayout from '/@/layouts/iframe/index.vue' ;
import { useRootSetting } from '/@/hooks/setting/useRootSetting' ;
import { useTransitionSetting } from '/@/hooks/setting/useTransitionSetting' ;
import { useCache } from './useCache' ;
import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting' ;
interface DefaultContext {
2020-12-11 21:42:25 +08:00
Component : FunctionalComponent & { type : { [ key : string ] : any } } ;
2020-12-04 21:25:33 +08:00
route : RouteLocation ;
}
export default defineComponent ( {
name : 'PageLayout' ,
setup() {
const { getCaches } = useCache ( true ) ;
const { getShowMultipleTab } = useMultipleTabSetting ( ) ;
const { getOpenKeepAlive , getCanEmbedIFramePage } = useRootSetting ( ) ;
const { getBasicTransition , getEnableTransition } = useTransitionSetting ( ) ;
const openCache = computed ( ( ) = > unref ( getOpenKeepAlive ) && unref ( getShowMultipleTab ) ) ;
return ( ) = > {
return (
2020-12-10 23:58:11 +08:00
< div >
2020-12-04 21:25:33 +08:00
< RouterView >
{ {
default : ( { Component , route } : DefaultContext ) = > {
// No longer show animations that are already in the tab
const cacheTabs = unref ( getCaches ) ;
const isInCache = cacheTabs . includes ( route . name as string ) ;
const name =
isInCache && route . meta . loaded && unref ( getEnableTransition )
? 'fade-slide'
: null ;
2020-12-11 21:42:25 +08:00
// When the child element is the parentView, adding the key will cause the component to be executed multiple times. When it is not parentView, you need to add a key, because it needs to be compatible with the same route carrying different parameters
const isParentView = Component ? . type . parentView ;
const componentKey = isParentView ? { } : { key : route.fullPath } ;
const renderComp = ( ) = > < Component { ...componentKey } / > ;
2020-12-04 21:25:33 +08:00
const PageContent = unref ( openCache ) ? (
2020-12-05 09:30:51 +08:00
< KeepAlive include = { cacheTabs } > { renderComp ( ) } < / KeepAlive >
2020-12-04 21:25:33 +08:00
) : (
renderComp ( )
) ;
return unref ( getEnableTransition ) ? (
< Transition
name = { name || route . meta . transitionName || unref ( getBasicTransition ) }
mode = "out-in"
appear = { true }
>
{ ( ) = > PageContent }
< / Transition >
) : (
PageContent
) ;
} ,
} }
< / RouterView >
{ unref ( getCanEmbedIFramePage ) && < FrameLayout / > }
2020-12-10 23:58:11 +08:00
< / div >
2020-12-04 21:25:33 +08:00
) ;
} ;
} ,
} ) ;