UNPKG

@tanstack/vue-router

Version:

Modern and scalable routing for Vue applications

31 lines (30 loc) 1.25 kB
import { useRouterState } from "./useRouterState.js"; import { injectDummyMatch, injectMatch } from "./matchContext.js"; import * as Vue from "vue"; //#region src/useMatch.tsx function useMatch(opts) { const nearestMatchId = opts.from ? injectDummyMatch() : injectMatch(); const pendingError = Vue.ref(null); const matchSelection = useRouterState({ select: (state) => { const match = state.matches.find((d) => opts.from ? opts.from === d.routeId : d.id === nearestMatchId.value); if (match === void 0) { if (state.pendingMatches?.find((d) => opts.from ? opts.from === d.routeId : d.id === nearestMatchId.value) || state.isTransitioning) { pendingError.value = null; return; } if (opts.shouldThrow ?? true) pendingError.value = /* @__PURE__ */ new Error(`Invariant failed: Could not find ${opts.from ? `an active match from "${opts.from}"` : "a nearest match!"}`); return; } pendingError.value = null; return opts.select ? opts.select(match) : match; } }); const result = Vue.computed(() => { if (pendingError.value) throw pendingError.value; return matchSelection.value; }); if (pendingError.value) throw pendingError.value; return result; } //#endregion export { useMatch }; //# sourceMappingURL=useMatch.js.map