-import { RouteRecordRaw, MatcherLocationRaw, MatcherLocation } from '../types'
+import {
+ RouteRecordRaw,
+ MatcherLocationRaw,
+ MatcherLocation,
+ isRouteName,
+ RouteRecordName,
+} from '../types'
import { createRouterError, ErrorTypes, MatcherError } from '../errors'
import { createRouteRecordMatcher, RouteRecordMatcher } from './path-matcher'
import { RouteRecordRedirect, RouteRecordNormalized } from './types'
addRoute: (record: RouteRecordRaw, parent?: RouteRecordMatcher) => () => void
removeRoute: {
(matcher: RouteRecordMatcher): void
- (name: Required<RouteRecordRaw>['name']): void
+ (name: RouteRecordName): void
}
getRoutes: () => RouteRecordMatcher[]
- getRecordMatcher: (
- name: Required<RouteRecordRaw>['name']
- ) => RouteRecordMatcher | undefined
+ getRecordMatcher: (name: RouteRecordName) => RouteRecordMatcher | undefined
resolve: (
location: MatcherLocationRaw,
currentLocation: MatcherLocation
): RouterMatcher {
// normalized ordered array of matchers
const matchers: RouteRecordMatcher[] = []
- const matcherMap = new Map<string | symbol, RouteRecordMatcher>()
+ const matcherMap = new Map<RouteRecordName, RouteRecordMatcher>()
- function getRecordMatcher(name: string) {
+ function getRecordMatcher(name: RouteRecordName) {
return matcherMap.get(name)
}
: noop
}
- function removeRoute(matcherRef: string | RouteRecordMatcher) {
- if (typeof matcherRef === 'string') {
+ function removeRoute(matcherRef: RouteRecordName | RouteRecordMatcher) {
+ if (isRouteName(matcherRef)) {
const matcher = matcherMap.get(matcherRef)
if (matcher) {
matcherMap.delete(matcherRef)
MatcherLocation,
RouteLocationNormalizedLoaded,
RouteLocation,
+ RouteRecordName,
+ isRouteName,
} from './types'
import { RouterHistory, HistoryState } from './history/common'
import {
history: RouterHistory
currentRoute: Ref<RouteLocationNormalizedLoaded>
- addRoute(parentName: string, route: RouteRecordRaw): () => void
+ addRoute(parentName: RouteRecordName, route: RouteRecordRaw): () => void
addRoute(route: RouteRecordRaw): () => void
- removeRoute(name: string): void
+ removeRoute(name: RouteRecordName): void
+ // TODO: hasRoute()
getRoutes(): RouteRecord[]
resolve(to: RouteLocationRaw): RouteLocation
const decodeParams = applyToParams.bind(null, decode)
function addRoute(
- parentOrRoute: string | RouteRecordRaw,
+ parentOrRoute: RouteRecordName | RouteRecordRaw,
route?: RouteRecordRaw
) {
let parent: Parameters<typeof matcher['addRoute']>[1] | undefined
let record: RouteRecordRaw
- if (typeof parentOrRoute === 'string') {
+ if (isRouteName(parentOrRoute)) {
parent = matcher.getRecordMatcher(parentOrRoute)
record = route!
} else {
return matcher.addRoute(record, parent)
}
- function removeRoute(name: string) {
+ function removeRoute(name: RouteRecordName) {
let recordMatcher = matcher.getRecordMatcher(name)
if (recordMatcher) {
matcher.removeRoute(recordMatcher)
} else if (__DEV__) {
// TODO: adapt if we allow Symbol as a name
- warn(`Cannot remove non-existent route "${name}"`)
+ warn(`Cannot remove non-existent route "${String(name)}"`)
}
}
}
export interface LocationAsName {
- name: string
+ name: RouteRecordName
params?: RouteParamsRaw
}
fullPath: string
query: LocationQuery
hash: string
- name: string | null | undefined
+ name: RouteRecordName | null | undefined
params: RouteParams
// TODO: make it an array?
redirectedFrom: RouteLocation | undefined
export type RouteComponent = ComponentOptions & RouteComponentInterface
export type RawRouteComponent = RouteComponent | Lazy<RouteComponent>
+export type RouteRecordName = string | symbol
+
// TODO: could this be moved to matcher?
/**
* Common properties among all kind of {@link RouteRecordRaw}
/**
* Name for the route record.
*/
- name?: string
+ name?: RouteRecordName
/**
* Allow passing down params as props to the component rendered by `router-view`.
*/
-import { RouteLocationRaw } from './index'
+import { RouteLocationRaw, RouteRecordName } from './index'
export function isRouteLocation(route: any): route is RouteLocationRaw {
return typeof route === 'string' || (route && typeof route === 'object')
}
+
+export function isRouteName(name: any): name is RouteRecordName {
+ return typeof name === 'string' || typeof name === 'symbol'
+}