Skip to content

Commit fb050b0

Browse files
decademoonyyx990803
authored andcommitted
Silence default child warning when redirecting (#1442)
* Silence default child warning when redirecting There is no need to warn about a named route that has a default child route if the named route intentionally redirects. * Updates create-map spec for redirecting route
1 parent e49c10e commit fb050b0

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/create-route-map.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ function addRouteRecord (
8383
}
8484

8585
if (route.children) {
86-
// Warn if route is named and has a default child route.
86+
// Warn if route is named, does not redirect and has a default child route.
8787
// If users navigate to this route by name, the default child will
8888
// not be rendered (GH Issue #629)
8989
if (process.env.NODE_ENV !== 'production') {
90-
if (route.name && route.children.some(child => /^\/?$/.test(child.path))) {
90+
if (route.name && !route.redirect && route.children.some(child => /^\/?$/.test(child.path))) {
9191
warn(
9292
false,
9393
`Named Route '${route.name}' has a default child route. ` +

test/unit/specs/create-map.spec.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ const routes = [
2323
name: 'bar.baz'
2424
}
2525
]
26+
},
27+
{
28+
path: '/bar-redirect',
29+
name: 'bar-redirect',
30+
redirect: { name: 'bar-redirect.baz' },
31+
component: Bar,
32+
children: [
33+
{
34+
path: '',
35+
component: Baz,
36+
name: 'bar-redirect.baz'
37+
}
38+
]
2639
}
2740
]
2841

@@ -44,7 +57,7 @@ describe('Creating Route Map', function () {
4457
})
4558

4659
it('has a pathList which places wildcards at the end', () => {
47-
expect(maps.pathList).toEqual(['', '/foo', '/bar/', '/bar', '*'])
60+
expect(maps.pathList).toEqual(['', '/foo', '/bar/', '/bar', '/bar-redirect/', '/bar-redirect', '*'])
4861
})
4962

5063
it('has a nameMap object for default subroute at \'bar.baz\'', function () {
@@ -54,7 +67,7 @@ describe('Creating Route Map', function () {
5467
it('in development, has logged a warning concerning named route of parent and default subroute', function () {
5568
process.env.NODE_ENV = 'development'
5669
maps = createRouteMap(routes)
57-
expect(console.warn).toHaveBeenCalled()
70+
expect(console.warn).toHaveBeenCalledTimes(1)
5871
expect(console.warn.calls.argsFor(0)[0]).toMatch('vue-router] Named Route \'bar\'')
5972
})
6073

0 commit comments

Comments
 (0)