From: Eduardo San Martin Morote Date: Thu, 12 Mar 2020 14:49:28 +0000 (+0100) Subject: fix(link): navigate to the alias path X-Git-Tag: v4.0.0-alpha.2~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=328411079e1aa8a5dc3903ae76a55d634946d9fd;p=thirdparty%2Fvuejs%2Frouter.git fix(link): navigate to the alias path --- diff --git a/__tests__/matcher/resolve.spec.ts b/__tests__/matcher/resolve.spec.ts index 01ca72e2..70be3614 100644 --- a/__tests__/matcher/resolve.spec.ts +++ b/__tests__/matcher/resolve.spec.ts @@ -156,7 +156,7 @@ describe('Router Matcher', () => { ) }) - it('resolves an alias with children', () => { + it('resolves an alias with children to the alias when using the path', () => { const children = [{ path: 'one', component, name: 'nested' }] assertRecordMatch( { @@ -182,6 +182,33 @@ describe('Router Matcher', () => { } ) }) + + it('resolves the original path of the named children of a route with an alias', () => { + const children = [{ path: 'one', component, name: 'nested' }] + assertRecordMatch( + { + path: '/parent', + alias: '/p', + component, + children, + }, + { name: 'nested' }, + { + path: '/parent/one', + name: 'nested', + params: {}, + matched: [ + { + path: '/parent', + children, + components, + aliasOf: undefined, + }, + { path: '/parent/one', name: 'nested', components }, + ], + } + ) + }) }) describe('LocationAsPath', () => { diff --git a/src/matcher/index.ts b/src/matcher/index.ts index 7ccf9d9a..97802d99 100644 --- a/src/matcher/index.ts +++ b/src/matcher/index.ts @@ -132,7 +132,7 @@ export function createRouterMatcher( // while (i < matchers.length && matcher.score <= matchers[i].score) i++ matchers.splice(i, 0, matcher) // only add the original record to the name map - if (matcher.record.name && !matcher.record.aliasOf) + if (matcher.record.name && !isAliasRecord(matcher)) matcherMap.set(matcher.record.name, matcher) } @@ -245,4 +245,17 @@ export function normalizeRouteRecord( } } +/** + * Checks if a record or any of its parent is an alias + * @param record + */ +function isAliasRecord(record: RouteRecordMatcher | undefined): boolean { + while (record) { + if (record.record.aliasOf) return true + record = record.parent + } + + return false +} + export { PathParserOptions } diff --git a/src/router.ts b/src/router.ts index 54fd5da3..ae9f18ae 100644 --- a/src/router.ts +++ b/src/router.ts @@ -184,15 +184,19 @@ export function createRouter({ } } - function push(to: RouteLocation): Promise { + function push( + to: RouteLocation | RouteLocationNormalized + ): Promise { return pushWithRedirect(to, undefined) } async function pushWithRedirect( - to: RouteLocation, + to: RouteLocation | RouteLocationNormalized, redirectedFrom: RouteLocationNormalized | undefined ): Promise { - const toLocation: RouteLocationNormalized = (pendingLocation = resolve(to)) + const toLocation: RouteLocationNormalized = (pendingLocation = + // Some functions will pass a normalized location and we don't need to resolve it again + typeof to === 'object' && 'matched' in to ? to : resolve(to)) const from: RouteLocationNormalized = currentRoute.value // @ts-ignore: no need to check the string as force do not exist on a string const force: boolean | undefined = to.force @@ -222,12 +226,18 @@ export function createRouter({ triggerError(error) } - finalizeNavigation(toLocation, from, true, to.replace === true) + finalizeNavigation( + toLocation, + from, + true, + // RouteLocationNormalized will give undefined + (to as RouteLocation).replace === true + ) return currentRoute.value } - function replace(to: RouteLocation) { + function replace(to: RouteLocation | RouteLocationNormalized) { const location = typeof to === 'string' ? { path: to } : to return push({ ...location, replace: true }) }