]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
feat: add aliasOf to normalized records
authorEduardo San Martin Morote <posva13@gmail.com>
Sun, 1 Mar 2020 18:31:17 +0000 (19:31 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Sun, 1 Mar 2020 18:31:17 +0000 (19:31 +0100)
__tests__/matcher/resolve.spec.ts
__tests__/utils.ts
playground/App.vue
playground/router.ts
src/matcher/index.ts
src/matcher/types.ts

index 088a272460028777845b4e23253c60ecbedc4935..01ca72e2e806ca04bd8e9d36bbc55ff577bda1cf 100644 (file)
@@ -48,7 +48,10 @@ describe('Router Matcher', () => {
           resolved.matched = record.map(normalizeRouteRecord)
         // allow passing an expect.any(Array)
         else if (Array.isArray(resolved.matched))
-          resolved.matched = resolved.matched.map(normalizeRouteRecord as any)
+          resolved.matched = resolved.matched.map(m => ({
+            ...normalizeRouteRecord(m as any),
+            aliasOf: m.aliasOf,
+          }))
       }
 
       // allows not passing params
@@ -60,7 +63,10 @@ describe('Router Matcher', () => {
 
       const startCopy = {
         ...start,
-        matched: start.matched.map(normalizeRouteRecord),
+        matched: start.matched.map(m => ({
+          ...normalizeRouteRecord(m),
+          aliasOf: m.aliasOf,
+        })),
       }
 
       // make matched non enumerable
@@ -111,6 +117,38 @@ describe('Router Matcher', () => {
                 path: '/home',
                 name: 'Home',
                 components,
+                aliasOf: expect.objectContaining({ name: 'Home', path: '/' }),
+                meta: { foo: true },
+              },
+            ],
+          }
+        )
+      })
+
+      it.todo('multiple aliases')
+      it.todo('resolve named child with parent with alias')
+
+      it('resolves the original record by name', () => {
+        assertRecordMatch(
+          {
+            path: '/',
+            alias: '/home',
+            name: 'Home',
+            components,
+            meta: { foo: true },
+          },
+          { name: 'Home' },
+          {
+            name: 'Home',
+            path: '/',
+            params: {},
+            meta: { foo: true },
+            matched: [
+              {
+                path: '/',
+                name: 'Home',
+                components,
+                aliasOf: undefined,
                 meta: { foo: true },
               },
             ],
@@ -133,7 +171,12 @@ describe('Router Matcher', () => {
             name: 'nested',
             params: {},
             matched: [
-              { path: '/p', children, components },
+              {
+                path: '/p',
+                children,
+                components,
+                aliasOf: expect.objectContaining({ path: '/parent' }),
+              },
               { path: '/p/one', name: 'nested', components },
             ],
           }
index 16299e7b81ce7225c21662777eaf3e6d3ab607fa..1b88eeb52b5ecc731fe7216739269798060a6313 100644 (file)
@@ -22,6 +22,7 @@ export interface RouteRecordViewLoose
     'path' | 'name' | 'components' | 'children' | 'meta' | 'beforeEnter'
   > {
   leaveGuards?: any
+  aliasOf: RouteRecordViewLoose | undefined
 }
 
 // @ts-ignore we are intentionally overriding the type
index 78823d42aac1c74a7a74d93617806edae5a3e7d4..5c6c567dfd8140fb11180bd6fcce5f2932faef19 100644 (file)
           >/nested/nested/nested</router-link
         >
       </li>
+      <li>
+        <router-link to="/anidado">/anidado</router-link>
+      </li>
+      <li>
+        <router-link to="/anidado/nested">/anidado/nested</router-link>
+      </li>
+      <li>
+        <router-link to="/anidado/nested/nested"
+          >/anidado/nested/nested</router-link
+        >
+      </li>
       <li>
         <router-link to="/long-0">/long-0</router-link>
       </li>
index 760a2cc7f0cc92e2d36116477092e8b8a4e1aff0..68d1a5fed110d6d51dbee414763a54e0a23e59e3 100644 (file)
@@ -49,6 +49,7 @@ export const router = createRouter({
     { path: '/:data(.*)', component: NotFound, name: 'NotFound' },
     {
       path: '/nested',
+      alias: '/anidado',
       component: Nested,
       name: 'Nested',
       children: [
index 0269bc771c94892404bcfa694bb88e9292493cb2..7ccf9d9a33270ad5893f11634f6a786cdfc1949b 100644 (file)
@@ -53,7 +53,6 @@ export function createRouterMatcher(
     const options: PathParserOptions = { ...globalOptions, ...record.options }
     // generate an array of records to correctly handle aliases
     const normalizedRecords: RouteRecordNormalized[] = [mainNormalizedRecord]
-    // TODO: remember aliases in records to allow active in router-link
     if ('alias' in record) {
       const aliases =
         typeof record.alias === 'string' ? [record.alias] : record.alias!
@@ -61,6 +60,7 @@ export function createRouterMatcher(
         normalizedRecords.push({
           ...mainNormalizedRecord,
           path: alias,
+          aliasOf: mainNormalizedRecord,
         })
       }
     }
@@ -131,7 +131,9 @@ export function createRouterMatcher(
     // console.log('END i is', { i })
     // while (i < matchers.length && matcher.score <= matchers[i].score) i++
     matchers.splice(i, 0, matcher)
-    if (matcher.record.name) matcherMap.set(matcher.record.name, matcher)
+    // only add the original record to the name map
+    if (matcher.record.name && !matcher.record.aliasOf)
+      matcherMap.set(matcher.record.name, matcher)
   }
 
   /**
@@ -187,6 +189,8 @@ export function createRouterMatcher(
     let parentMatcher: RouteRecordMatcher | void = matcher
     while (parentMatcher) {
       // reversed order so parents are at the beginning
+      // const { record } = parentMatcher
+      // TODO: check resolving child routes by path when parent has an alias
       matched.unshift(parentMatcher.record)
       parentMatcher = parentMatcher.parent
     }
@@ -237,6 +241,7 @@ export function normalizeRouteRecord(
     beforeEnter,
     meta: record.meta || {},
     leaveGuards: [],
+    aliasOf: undefined,
   }
 }
 
index 5de1ca03d08e363b18ddc48beb9c3ae78a7f8957..58e2603901b5b57595347e0f5ca4cc4e08337557 100644 (file)
@@ -9,4 +9,5 @@ export interface RouteRecordNormalized {
   meta: Exclude<RouteRecordMultipleViews['meta'], void>
   beforeEnter: RouteRecordMultipleViews['beforeEnter']
   leaveGuards: NavigationGuard[]
+  aliasOf: RouteRecordNormalized | undefined
 }