8000 refactor(router): drop forRoot guard in production by arturovt · Pull Request #59458 · angular/angular · GitHub
[go: up one dir, main page]

Skip to content

refactor(router): drop forRoot guard in production #59458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions goldens/public-api/router/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -861,11 +861,11 @@ export class RouterLinkActive implements OnChanges, OnDestroy, AfterContentInit

// @public
export class RouterModule {
constructor(guard: any);
constructor();
static forChild(routes: Routes): ModuleWithProviders<RouterModule>;
static forRoot(routes: Routes, config?: ExtraOptions): ModuleWithProviders<RouterModule>;
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<RouterModule, [{ optional: true; }]>;
static ɵfac: i0.ɵɵFactoryDeclaration<RouterModule, never>;
// (undocumented)
static ɵinj: i0.ɵɵInjectorDeclaration<RouterModule>;
// (undocumented)
Expand Down
24 changes: 14 additions & 10 deletions packages/router/src/router 10000 _module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ const ROUTER_DIRECTIVES = [RouterOutlet, RouterLink, RouterLinkActive, EmptyOutl
* @docsNotRequired
*/
export const ROUTER_FORROOT_GUARD = new InjectionToken<void>(
typeof ngDevMode === 'undefined' || ngDevMode
? 'router duplicate forRoot guard'
: 'ROUTER_FORROOT_GUARD',
typeof ngDevMode === 'undefined' || ngDevMode ? 'router duplicate forRoot guard' : '',
);

// TODO(atscott): All of these except `ActivatedRoute` are `providedIn: 'root'`. They are only kept
Expand Down Expand Up @@ -112,7 +110,11 @@ export const ROUTER_PROVIDERS: Provider[] = [
exports: ROUTER_DIRECTIVES,
})
export class RouterModule {
constructor(@Optional() @Inject(ROUTER_FORROOT_GUARD) guard: any) {}
constructor() {
if (typeof ngDevMode === 'undefined' || ngDevMode) {
inject(ROUTER_FORROOT_GUARD, {optional: true});
}
}

/**
* Creates and configures a module with all the router providers and directives.
Expand Down Expand Up @@ -143,11 +145,13 @@ export class RouterModule {
: []
: [],
{provide: ROUTES, multi: true, useValue: routes},
{
provide: ROUTER_FORROOT_GUARD,
useFactory: provideForRootGuard,
deps: [[Router, new Optional(), new SkipSelf()]],
},
typeof ngDevMode === 'undefined' || ngDevMode
? {
provide: ROUTER_FORROOT_GUARD,
useFactory: provideForRootGuard,
deps: [[Router, new Optional(), new SkipSelf()]],
}
: [],
config?.errorHandler
? {
provide: NAVIGATION_ERROR_HANDLER,
Expand Down Expand Up @@ -224,7 +228,7 @@ function providePathLocationStrategy(): Provider {
}

export function provideForRootGuard(router: Router): any {
if ((typeof ngDevMode === 'undefined' || ngDevMode) && router) {
if (router) {
throw new RuntimeError(
RuntimeErrorCode.FOR_ROOT_CALLED_TWICE,
`The Router was provided more than once. This can happen if 'forRoot' is used outside of the root injector.` +
Expand Down
Loading
0