From: Eduardo San Martin Morote Date: Wed, 8 Apr 2020 12:14:38 +0000 (+0200) Subject: feat(router): hasRoute X-Git-Tag: v4.0.0-alpha.6~54 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ca02444c91c8f6b21caf6a71dee5d0f2e3f7e51b;p=thirdparty%2Fvuejs%2Frouter.git feat(router): hasRoute --- diff --git a/__tests__/router.spec.ts b/__tests__/router.spec.ts index ccfaf41e..51057338 100644 --- a/__tests__/router.spec.ts +++ b/__tests__/router.spec.ts @@ -548,6 +548,7 @@ describe('Router', () => { }) }) }) + describe('Dynamic Routing', () => { it('resolves new added routes', async () => { const { router } = await newRouter() @@ -565,6 +566,19 @@ describe('Router', () => { }) }) + it('checks if a route exists', async () => { + const { router } = await newRouter() + router.addRoute({ + name: 'new-route', + path: '/new-route', + component: components.Foo, + }) + expect(router.hasRoute('new-route')).toBe(true) + expect(router.hasRoute('no')).toBe(false) + router.removeRoute('new-route') + expect(router.hasRoute('new-route')).toBe(false) + }) + it('can redirect to children in the middle of navigation', async () => { const { router } = await newRouter() expect(router.resolve('/new-route')).toMatchObject({ diff --git a/src/router.ts b/src/router.ts index e1047fa3..bd4a8202 100644 --- a/src/router.ts +++ b/src/router.ts @@ -87,7 +87,7 @@ export interface Router { addRoute(parentName: RouteRecordName, route: RouteRecordRaw): () => void addRoute(route: RouteRecordRaw): () => void removeRoute(name: RouteRecordName): void - // TODO: hasRoute() + hasRoute(name: RouteRecordName): boolean getRoutes(): RouteRecord[] resolve(to: RouteLocationRaw): RouteLocation @@ -163,6 +163,10 @@ export function createRouter({ return matcher.getRoutes().map(routeMatcher => routeMatcher.record) } + function hasRoute(name: RouteRecordName): boolean { + return !!matcher.getRecordMatcher(name) + } + function resolve( location: RouteLocationRaw, currentLocation?: RouteLocationNormalizedLoaded @@ -545,6 +549,7 @@ export function createRouter({ addRoute, removeRoute, + hasRoute, getRoutes, push,