From 34a4ef04a3f81b0c10808e4d5bf565acebb60e1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B3=B0?= Date: Thu, 1 Nov 2018 12:02:33 +0800 Subject: [PATCH 1/4] Fix the error caused by the same path and alias of RouteConfig --- src/create-route-map.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/create-route-map.js b/src/create-route-map.js index 560e05fdd..af115f8b1 100644 --- a/src/create-route-map.js +++ b/src/create-route-map.js @@ -112,6 +112,11 @@ function addRouteRecord ( }) } + if (!pathMap[record.path]) { + pathList.push(record.path) + pathMap[record.path] = record + } + if (route.alias !== undefined) { const aliases = Array.isArray(route.alias) ? route.alias @@ -133,11 +138,6 @@ function addRouteRecord ( }) } - if (!pathMap[record.path]) { - pathList.push(record.path) - pathMap[record.path] = record - } - if (name) { if (!nameMap[name]) { nameMap[name] = record From 635135ff3f50b7c8e97b6a6cb6349b88ce509cff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B3=B0?= Date: Thu, 8 Nov 2018 10:43:30 +0800 Subject: [PATCH 2/4] warning path with the same value as an alias --- src/create-route-map.js | 42 ++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/create-route-map.js b/src/create-route-map.js index af115f8b1..09c887f40 100644 --- a/src/create-route-map.js +++ b/src/create-route-map.js @@ -118,24 +118,32 @@ function addRouteRecord ( } if (route.alias !== undefined) { - const aliases = Array.isArray(route.alias) - ? route.alias - : [route.alias] - - aliases.forEach(alias => { - const aliasRoute = { - path: alias, - children: route.children - } - addRouteRecord( - pathList, - pathMap, - nameMap, - aliasRoute, - parent, - record.path || '/' // matchAs + if (route.alias === path) { + warn( + false, + `Path with the same value as an alias: ` + + `{ name: "${name}", path: "${path}" }` ) - }) + } else { + const aliases = Array.isArray(route.alias) + ? route.alias + : [route.alias] + + aliases.forEach(alias => { + const aliasRoute = { + path: alias, + children: route.children + } + addRouteRecord( + pathList, + pathMap, + nameMap, + aliasRoute, + parent, + record.path || '/' // matchAs + ) + }) + } } if (name) { From 53e062f89efc09a53fbd06807251160396826c9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B3=B0?= Date: Thu, 8 Nov 2018 10:53:24 +0800 Subject: [PATCH 3/4] fix flow check --- src/create-route-map.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/create-route-map.js b/src/create-route-map.js index 09c887f40..1731517b2 100644 --- a/src/create-route-map.js +++ b/src/create-route-map.js @@ -122,7 +122,7 @@ function addRouteRecord ( warn( false, `Path with the same value as an alias: ` + - `{ name: "${name}", path: "${path}" }` + `{ path: "${path}" }` ) } else { const aliases = Array.isArray(route.alias) From 05c90b6120103efa4591944c51ecc6c9c696edca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B3=B0?= Date: Tue, 20 Nov 2018 15:14:18 +0800 Subject: [PATCH 4/4] path alias same warning in development --- src/create-route-map.js | 13 ++++++------- test/unit/specs/create-map.spec.js | 12 ++++++++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/create-route-map.js b/src/create-route-map.js index 1731517b2..826a8125f 100644 --- a/src/create-route-map.js +++ b/src/create-route-map.js @@ -118,13 +118,7 @@ function addRouteRecord ( } if (route.alias !== undefined) { - if (route.alias === path) { - warn( - false, - `Path with the same value as an alias: ` + - `{ path: "${path}" }` - ) - } else { + if (route.alias !== path) { const aliases = Array.isArray(route.alias) ? route.alias : [route.alias] @@ -143,6 +137,11 @@ function addRouteRecord ( record.path || '/' // matchAs ) }) + } else if (process.env.NODE_ENV !== 'production') { + warn( + false, + `Path with the same value as an alias: "${path}"` + ) } } diff --git a/test/unit/specs/create-map.spec.js b/test/unit/specs/create-map.spec.js index 67fba131a..d0d451866 100644 --- a/test/unit/specs/create-map.spec.js +++ b/test/unit/specs/create-map.spec.js @@ -97,6 +97,18 @@ describe('Creating Route Map', function () { expect(console.warn.calls.argsFor(0)[0]).toMatch('vue-router] Duplicate param keys in route with path: "/foo/:id/bar/:id"') }) + it('in development, warn path with the same value as an alias', () => { + process.env.NODE_ENV = 'development' + maps = createRouteMap([ + { + path: '/foo-alias', component: Foo, + alias: '/foo-alias' + } + ]) + expect(console.warn).toHaveBeenCalled() + expect(console.warn.calls.argsFor(0)[0]).toMatch('vue-router] Path with the same value as an alias: "/foo-alias"') + }) + describe('path-to-regexp options', function () { const routes = [ { path: '/foo', name: 'foo', component: Foo },