From 93289f9c7639f9d455d27da113aa1dc132c6284d Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Thu, 11 Aug 2022 09:00:21 -0700 Subject: [PATCH] refactor(router): Update error message when Router is provided twice (#47130) The current error message is absolute in that it thinks there is only one possible way to provide Router twice. In fact, you can get a new instance of the Router in several ways so the error message should indicate the exact failure case with a _potential_ cause. Based on findings in thread https://github.com/angular/angular/commit/0cbbd6aeecda8ea994f1086727e580b813a53d79#commitcomment-80900192 PR Close #47130 --- packages/router/src/router_module.ts | 3 ++- packages/router/test/integration.spec.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/router/src/router_module.ts b/packages/router/src/router_module.ts index 13d6665e9ef..803fb2de477 100644 --- a/packages/router/src/router_module.ts +++ b/packages/router/src/router_module.ts @@ -181,7 +181,8 @@ export function provideForRootGuard(router: Router): any { if (NG_DEV_MODE && router) { throw new RuntimeError( RuntimeErrorCode.FOR_ROOT_CALLED_TWICE, - `RouterModule.forRoot() called twice. Lazy loaded modules should use RouterModule.forChild() instead.`); + `The Router was provided more than once. This can happen if 'forRoot' is used outside of the root injector.` + + ` Lazy loaded modules should use RouterModule.forChild() instead.`); } return 'guarded'; } diff --git a/packages/router/test/integration.spec.ts b/packages/router/test/integration.spec.ts index b07b0b36975..12cafce2b79 100644 --- a/packages/router/test/integration.spec.ts +++ b/packages/router/test/integration.spec.ts @@ -5806,7 +5806,7 @@ describe('Integration', () => { let recordedError: any = null; router.navigateByUrl('/lazy/loaded')!.catch(err => recordedError = err); advance(fixture); - expect(recordedError.message).toContain(`RouterModule.forRoot() called twice.`); + expect(recordedError.message).toContain(`NG04007`); }))); it('should combine routes from multiple modules into a single configuration',