From 68636d9689143a7b3019fd9b94d05e107f21b580 Mon Sep 17 00:00:00 2001 From: Albert Lucianto Date: Fri, 2 Feb 2018 19:00:01 +0800 Subject: [PATCH 1/3] throw error when component is undefined or null --- src/create-route-map.js | 4 ++++ test/unit/specs/create-map.spec.js | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/src/create-route-map.js b/src/create-route-map.js index 560e05fdd..dd014cc49 100644 --- a/src/create-route-map.js +++ b/src/create-route-map.js @@ -57,6 +57,10 @@ function addRouteRecord ( `route config "component" for path: ${String(path || name)} cannot be a ` + `string id. Use an actual component instead.` ) + assert( + route.component !== null && route.component !== undefined, + `route config "component" for path: ${String(path || name)} cannot be ${String(route.component)}.` + ) } const pathToRegexpOptions: PathToRegexpOptions = route.pathToRegexpOptions || {} diff --git a/test/unit/specs/create-map.spec.js b/test/unit/specs/create-map.spec.js index 67fba131a..cd632e4ed 100644 --- a/test/unit/specs/create-map.spec.js +++ b/test/unit/specs/create-map.spec.js @@ -78,6 +78,13 @@ describe('Creating Route Map', function () { }).toThrowError(/"path" is required/) }) + it('in development, throws if component is null or undefined', function () { + process.env.NODE_ENV = 'development' + expect(() => { + maps = createRouteMap([{ path: '/' }]) + }).toThrowError(/route config "component" for path/) + }) + it('in production, it has not logged this warning', function () { maps = createRouteMap(routes) expect(console.warn).not.toHaveBeenCalled() From 07c2e73fbf1f95b13d9e8f51bfaf85f3eb24121e Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 2 Feb 2018 12:14:40 +0100 Subject: [PATCH 2/3] Update create-route-map.js --- 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 dd014cc49..e5250d175 100644 --- a/src/create-route-map.js +++ b/src/create-route-map.js @@ -58,7 +58,7 @@ function addRouteRecord ( `string id. Use an actual component instead.` ) assert( - route.component !== null && route.component !== undefined, + route.component, `route config "component" for path: ${String(path || name)} cannot be ${String(route.component)}.` ) } From 60a3d9876dcaf08181b3b07d751dbb5b55fe8142 Mon Sep 17 00:00:00 2001 From: Albert Lucianto Date: Fri, 2 Feb 2018 20:19:05 +0800 Subject: [PATCH 3/3] fix false error when it is not just a component --- src/create-route-map.js | 14 +++++++++----- test/unit/specs/create-map.spec.js | 5 ++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/create-route-map.js b/src/create-route-map.js index e5250d175..9dc572cf2 100644 --- a/src/create-route-map.js +++ b/src/create-route-map.js @@ -47,7 +47,8 @@ function addRouteRecord ( nameMap: Dictionary, route: RouteConfig, parent?: RouteRecord, - matchAs?: string + matchAs?: string, + isAnAlias?: boolean ) { const { path, name } = route if (process.env.NODE_ENV !== 'production') { @@ -57,9 +58,11 @@ function addRouteRecord ( `route config "component" for path: ${String(path || name)} cannot be a ` + `string id. Use an actual component instead.` ) - assert( - route.component, - `route config "component" for path: ${String(path || name)} cannot be ${String(route.component)}.` + warn( + route.component || route.components || route.redirect || route.beforeEnter || isAnAlias, + `route config "component" for path: ${String(path || name)} should be either a ` + + `component (or named component), redirection, navigation guard, or an alias ` + + `to another root.` ) } @@ -132,7 +135,8 @@ function addRouteRecord ( nameMap, aliasRoute, parent, - record.path || '/' // matchAs + record.path || '/', // matchAs + true ) }) } diff --git a/test/unit/specs/create-map.spec.js b/test/unit/specs/create-map.spec.js index cd632e4ed..c838666ab 100644 --- a/test/unit/specs/create-map.spec.js +++ b/test/unit/specs/create-map.spec.js @@ -80,9 +80,8 @@ describe('Creating Route Map', function () { it('in development, throws if component is null or undefined', function () { process.env.NODE_ENV = 'development' - expect(() => { - maps = createRouteMap([{ path: '/' }]) - }).toThrowError(/route config "component" for path/) + maps = createRouteMap([{ path: '/' }]) + expect(console.warn).toHaveBeenCalled() }) it('in production, it has not logged this warning', function () {