import * as runtimeDom from '@vue/runtime-dom'
import { compile } from '@vue/compiler-dom'
import { Router } from '../src'
+import { routerKey, routeLocationKey } from '../src/utils/injectionSymbols'
export function mount(
router: Router,
const { template, components, ...ComponentWithoutTemplate } = Component
const app = createApp(ComponentWithoutTemplate as any, rootProps)
- app.provide('router', router)
- app.provide('route', router.currentRoute)
+ app.provide(routerKey, router)
+ app.provide(routeLocationKey, router.currentRoute)
for (const componentName in components) {
app.component(componentName, components[componentName])
} from 'vue'
import { RouteLocation, RouteLocationNormalized, Immutable } from '../types'
import { isSameLocationObject, isSameRouteRecord } from '../utils'
-import { routerKey } from '../injectKeys'
+import { routerKey } from '../utils/injectionSymbols'
type VueUseOptions<T> = {
[k in keyof T]: Ref<T[k]> | T[k]
export function useLink(props: UseLinkOptions) {
const router = inject(routerKey)!
+ const currentRoute = router.currentRoute
const route = computed(() => router.resolve(unref(props.to)))
const href = computed(() => router.createHref(route.value))
const activeRecordIndex = computed<number>(() => {
// TODO: handle children with empty path: they should relate to their parent
const currentMatched = route.value.matched[route.value.matched.length - 1]
- return router.currentRoute.value.matched.findIndex(
+ return currentRoute.value.matched.findIndex(
isSameRouteRecord.bind(null, currentMatched)
)
})
const isActive = computed<boolean>(
() =>
activeRecordIndex.value > -1 &&
- includesParams(router.currentRoute.value.params, route.value.params)
+ includesParams(currentRoute.value.params, route.value.params)
)
const isExactActive = computed<boolean>(
() =>
- activeRecordIndex.value ===
- router.currentRoute.value.matched.length - 1 &&
- isSameLocationObject(router.currentRoute.value.params, route.value.params)
+ activeRecordIndex.value === currentRoute.value.matched.length - 1 &&
+ isSameLocationObject(currentRoute.value.params, route.value.params)
)
// TODO: handle replace prop
defineComponent,
PropType,
computed,
- InjectionKey,
ref,
ComponentPublicInstance,
- ComputedRef,
} from 'vue'
-import { routeKey } from '../injectKeys'
import { RouteLocationMatched } from '../types'
-
-// TODO: make it work with no symbols too for IE
-export const matchedRouteKey = Symbol() as InjectionKey<
- ComputedRef<RouteLocationMatched | undefined>
->
+import {
+ matchedRouteKey,
+ viewDepthKey,
+ routeLocationKey,
+} from '../utils/injectionSymbols'
export const View = defineComponent({
name: 'RouterView',
},
setup(props, { attrs }) {
- const route = inject(routeKey)!
- const depth: number = inject('routerViewDepth', 0)
- provide('routerViewDepth', depth + 1)
+ const route = inject(routeLocationKey)!
+ const depth: number = inject(viewDepthKey, 0)
+ provide(viewDepthKey, depth + 1)
const matchedRoute = computed(
() => route.value.matched[depth] as RouteLocationMatched | undefined
import createWebHistory from './history/html5'
import createMemoryHistory from './history/memory'
import createWebHashHistory from './history/hash'
+import { inject } from 'vue'
+import { routerKey, routeLocationKey } from './utils/injectionSymbols'
export {
RouteLocationNormalized,
export { createRouter, Router, RouterOptions } from './router'
export { onBeforeRouteLeave } from './navigationGuards'
-export { useRoute, useRouter } from './injectKeys'
export { Link } from './components/Link'
export { View } from './components/View'
export { createWebHistory, createMemoryHistory, createWebHashHistory }
+
+export function useRouter() {
+ return inject(routerKey)!
+}
+
+export function useRoute() {
+ return inject(routeLocationKey)!
+}
+++ /dev/null
-import { InjectionKey, Ref, inject } from 'vue'
-import { Router, RouteLocationNormalized } from '.'
-import { RouteLocationNormalizedResolved } from './types'
-
-export const routerKey = ('router' as unknown) as InjectionKey<Router>
-export const routeKey = ('route' as unknown) as InjectionKey<
- Ref<RouteLocationNormalizedResolved>
->
-
-export function useRouter(): Router {
- return inject(routerKey)!
-}
-
-export function useRoute(): Ref<RouteLocationNormalized> {
- return inject(routeKey)!
-}
import { NavigationGuard } from './types'
import { inject, getCurrentInstance, warn } from 'vue'
-import { matchedRouteKey } from './components/View'
import { RouteRecordNormalized } from './matcher/types'
+import { matchedRouteKey } from './utils/injectionSymbols'
export function onBeforeRouteLeave(leaveGuard: NavigationGuard) {
const instance = getCurrentInstance()
import { RouteRecordNormalized } from './matcher/types'
import { Link } from './components/Link'
import { View } from './components/View'
+import { routerKey, routeLocationKey } from './utils/injectionSymbols'
type ErrorHandler = (error: any) => any
// resolve, reject arguments of Promise constructor
})
// TODO: merge strats?
- app.provide('router', router)
- app.provide('route', router.currentRoute)
+ app.provide(routerKey, router)
+ app.provide(routeLocationKey, router.currentRoute)
}
async function runGuardQueue(guards: Lazy<any>[]): Promise<void> {
props?:
| boolean
| Record<string, any>
- | ((to: RouteLocationNormalized) => Record<string, any>)
+ | ((to: Immutable<RouteLocationNormalized>) => Record<string, any>)
// TODO: beforeEnter has no effect with redirect, move and test
beforeEnter?: NavigationGuard<undefined> | NavigationGuard<undefined>[]
meta?: Record<string | number | symbol, any>
import { guardToPromiseFn } from './guardToPromiseFn'
import { RouteRecordNormalized } from '../matcher/types'
import { LocationQueryValue } from './query'
+import { hasSymbol } from './injectionSymbols'
export * from './guardToPromiseFn'
-const hasSymbol =
- typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol'
-
function isESModule(obj: any): obj is { default: RouteComponent } {
return obj.__esModule || (hasSymbol && obj[Symbol.toStringTag] === 'Module')
}
--- /dev/null
+import { InjectionKey, ComputedRef, Ref } from 'vue'
+import {
+ RouteLocationMatched,
+ Immutable,
+ RouteLocationNormalizedResolved,
+} from '../types'
+import { Router } from '../router'
+
+export const hasSymbol =
+ typeof Symbol === 'function' && typeof Symbol.toStringTag === 'symbol'
+
+export const PolySymbol = (name: string) =>
+ // vr = vue router
+ hasSymbol ? Symbol(name) : `_vr_` + name
+
+// rvlm = Router View Location Matched
+export const matchedRouteKey = PolySymbol('rvlm') as InjectionKey<
+ ComputedRef<RouteLocationMatched | undefined>
+>
+// rvd = Router View Depth
+export const viewDepthKey = PolySymbol('rvd') as InjectionKey<number>
+
+// r = router
+export const routerKey = PolySymbol('r') as InjectionKey<Router>
+// rt = route location
+export const routeLocationKey = PolySymbol('rl') as InjectionKey<
+ Ref<Immutable<RouteLocationNormalizedResolved>>
+>