{ setup(props, { attrs, slots }) { const injectedRoute = inject(routerViewLocationKey)! const routeToDisplay = computed<RouteLocationNormalizedLoaded>( () => props.route || injectedRoute.value ) const injectedDepth = inject(viewDepthKey, 0) // The depth changes based on empty components option, which allows passthrough routes e.g. routes with children // that are used to reuse the `path` property const depth = computed<number>(() => { let initialDepth = unref(injectedDepth) const { matched } = routeToDisplay.value letmatchedRoute: RouteLocationMatched | undefined while ( (matchedRoute = matched[initialDepth]) && !matchedRoute.components ) { initialDepth++ } return initialDepth }) const matchedRouteRef = computed<RouteLocationMatched | undefined>( () => routeToDisplay.value.matched[depth.value] )
provide( viewDepthKey, computed(() => depth.value + 1) ) provide(matchedRouteKey, matchedRouteRef) provide(routerViewLocationKey, routeToDisplay) // ... return() => { const route = routeToDisplay.value // we need the value at the time we render because when we unmount, we // navigated to a different location so the value is different const currentName = props.name const matchedRoute = matchedRouteRef.value constViewComponent = matchedRoute && matchedRoute.components![currentName]
if (!ViewComponent) { returnnormalizeSlot(slots.default, { Component: ViewComponent, route }) }
// this initial navigation is only necessary on client, on server it doesn't // make sense because it will create an extra unnecessary navigation and could // lead to problems if ( isBrowser && // used for the initial navigation client side to avoid pushing // multiple times when the router is used in multiple apps !started && currentRoute.value === START_LOCATION_NORMALIZED ) { // see above started = true push(routerHistory.location).catch(err => { if (__DEV__) warn('Unexpected error when starting the router:', err) }) }
functionfinalizeNavigation(toLocation, from, isPush, replace, data) { // ... const isFirstNavigation = from === START_LOCATION_NORMALIZED; const state = !isBrowser ? {} : history.state; // change URL only if the user did a push/replace and if it's not the initial navigation because // it's just reflecting the url if (isPush) { // on the initial navigation, we want to reuse the scroll position from // history state if it exists if (replace || isFirstNavigation) routerHistory.replace(toLocation.fullPath, assign({ scroll: isFirstNavigation && state && state.scroll, }, data)); else routerHistory.push(toLocation.fullPath, data); } // accept current navigation currentRoute.value = toLocation; handleScroll(toLocation, from, isPush, isFirstNavigation); markAsReady(); }