UNPKG

vue-router

Version:
157 lines (156 loc) • 6.09 kB
/*! * vue-router v5.5.3 * (c) 2026 Eduardo San Martin Morote * @license MIT */ import { a as routerViewOnRouteRenderedKey, f as assign, h as isArray, i as routerViewLocationKey, n as routeLocationKey, o as viewDepthKey, s as diagnostics, t as matchedRouteKey } from "./injectionSymbols-CGHHjbrE.js"; import { O as isBrowser, c as START_LOCATION_NORMALIZED, f as isSameRouteRecord, l as createReactiveRoute } from "./RouterLink-TyasWdjU.js"; import { computed, defineComponent, getCurrentInstance, h, inject, nextTick, provide, shallowRef, unref, watch } from "vue"; // src/RouterView.ts /** * Props of the VDOM and Vapor `RouterView`. * @internal */ const routerViewProps = { name: { type: String, default: "default" }, route: Object }; /** * State shared by the VDOM and Vapor `RouterView`. * @internal */ function useRouterViewState(props) { const injectedRoute = inject(routerViewLocationKey); const routeToDisplay = computed(() => props.route || injectedRoute.value); const injectedDepth = inject(viewDepthKey, 0); const depth = computed(() => { let initialDepth = unref(injectedDepth); const { matched } = routeToDisplay.value; let matchedRoute; while ((matchedRoute = matched[initialDepth]) && !matchedRoute.components) initialDepth++; return initialDepth; }); const matchedRouteRef = computed(() => routeToDisplay.value.matched[depth.value]); provide(viewDepthKey, computed(() => depth.value + 1)); provide(matchedRouteKey, matchedRouteRef); provide(routerViewLocationKey, routeToDisplay); provide(routeLocationKey, createReactiveRoute(routeToDisplay)); const onRouteRenderedCallbacks = /* @__PURE__ */ new Set(); provide(routerViewOnRouteRenderedKey, onRouteRenderedCallbacks); const viewRef = shallowRef(); let settledRoute = START_LOCATION_NORMALIZED; watch(() => [ viewRef.value, matchedRouteRef.value, props.name ], ([instance, to, name], [oldInstance, from, _oldName]) => { if (to) { if (from && from !== to && instance && instance === oldInstance) { if (!to.leaveGuards.size) to.leaveGuards = from.leaveGuards; if (!to.updateGuards.size) to.updateGuards = from.updateGuards; } } if (__VUE_OPTIONS_API__ && instance && to && (!from || !isSameRouteRecord(to, from) || !oldInstance)) (to.enterCallbacks[name] || []).forEach((callback) => callback(instance)); }, { flush: "post" }); /** * Calls the `onRouteRendered()` callbacks if `route` is displayed. Call it * in a `nextTick()` after the view is in the document: other views and hooks * of the same flush (nested RouterView, onActivated) run before it. * * @param route - route being displayed, read before the `nextTick()` */ const settle = (route) => { if (settledRoute !== route && routeToDisplay.value === route) { const from = settledRoute; settledRoute = route; for (const callback of onRouteRenderedCallbacks) callback(route, from); } }; return process.env.NODE_ENV !== "production" || __VUE_PROD_DEVTOOLS__ ? [ routeToDisplay, matchedRouteRef, viewRef, settle, depth ] : [ routeToDisplay, matchedRouteRef, viewRef, settle ]; } const RouterViewImpl = /*#__PURE__*/ defineComponent({ name: "RouterView", inheritAttrs: false, props: routerViewProps, compatConfig: { MODE: 3 }, setup(props, { attrs, slots }) { process.env.NODE_ENV !== "production" && warnDeprecatedUsage(); const [routeToDisplay, matchedRouteRef, viewRef, settle, depth] = useRouterViewState(props); const { app } = getCurrentInstance().appContext; return () => { const route = routeToDisplay.value; const currentName = props.name; const matchedRoute = matchedRouteRef.value; const ViewComponent = matchedRoute && matchedRoute.components[currentName]; if (!ViewComponent) return normalizeSlot(slots.default, { Component: ViewComponent, route }); const onVnodeSettled = (vnode) => { if (__VUE_OPTIONS_API__) matchedRoute.instances[currentName] = viewRef.value; nextTick(() => { while (!vnode.el && vnode.component) vnode = vnode.component.subTree; if (vnode.el?.getRootNode() === app._container.getRootNode()) settle(route); }); }; const routePropsOption = matchedRoute.props[currentName]; const routeProps = routePropsOption ? routePropsOption === true ? route.params : typeof routePropsOption === "function" ? routePropsOption(route) : routePropsOption : null; const component = h(ViewComponent, assign({}, routeProps, attrs, { onVnodeUnmounted: __VUE_OPTIONS_API__ ? (vnode) => { if (vnode.component.isUnmounted) matchedRoute.instances[currentName] = null; } : void 0, onVnodeMounted: onVnodeSettled, onVnodeUpdated: onVnodeSettled, ref: viewRef })); if ((process.env.NODE_ENV !== "production" || __VUE_PROD_DEVTOOLS__) && isBrowser && component.ref) { const info = { depth: depth.value, name: matchedRoute.name, path: matchedRoute.path, meta: matchedRoute.meta }; (isArray(component.ref) ? component.ref.map((r) => r.i) : [component.ref.i]).forEach((instance) => { if (instance) instance.__vrv_devtools = info; }); } return normalizeSlot(slots.default, { Component: component, route }) || component; }; } }); function normalizeSlot(slot, data) { if (!slot) return null; const slotContent = slot(data); return slotContent.length === 1 ? slotContent[0] : slotContent; } /** * Component to display the current route the user is at. */ const RouterView = RouterViewImpl; function warnDeprecatedUsage() { const instance = getCurrentInstance(); const parentName = instance.parent && instance.parent.type.name; const parentSubTreeType = instance.parent && instance.parent.subTree && instance.parent.subTree.type; if (parentName && (parentName === "KeepAlive" || parentName.includes("Transition")) && typeof parentSubTreeType === "object" && parentSubTreeType.name === "RouterView") { const comp = parentName === "KeepAlive" ? "keep-alive" : "transition"; diagnostics.VUE_ROUTER_R0060({ comp }); } } export { routerViewProps as n, useRouterViewState as r, RouterView as t };