Skip to content

Commit

Permalink
feat(router): prevent provideRouter() from usage in @component (#47669
Browse files Browse the repository at this point in the history
)

This commit switches `provideRouter()` to return the new
`EnvironmentProviders` wrapper type, preventing it from being accidentally
(or intentionally) included in `@Component.providers`.

PR Close #47669
  • Loading branch information
alxhub authored and thePunderWoman committed Oct 7, 2022
1 parent 7de1469 commit 07017a7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion goldens/public-api/router/index.md
Expand Up @@ -524,7 +524,7 @@ export abstract class PreloadingStrategy {
export const PRIMARY_OUTLET = "primary";

// @public
export function provideRouter(routes: Routes, ...features: RouterFeatures[]): Provider[];
export function provideRouter(routes: Routes, ...features: RouterFeatures[]): EnvironmentProviders;

// @public
export function provideRoutes(routes: Routes): Provider[];
Expand Down
8 changes: 4 additions & 4 deletions packages/router/src/provide_router.ts
Expand Up @@ -7,7 +7,7 @@
*/

import {LOCATION_INITIALIZED, ViewportScroller} from '@angular/common';
import {APP_BOOTSTRAP_LISTENER, APP_INITIALIZER, ApplicationRef, ComponentRef, ENVIRONMENT_INITIALIZER, inject, InjectFlags, InjectionToken, Injector, Provider, Type} from '@angular/core';
import {APP_BOOTSTRAP_LISTENER, APP_INITIALIZER, ApplicationRef, ComponentRef, ENVIRONMENT_INITIALIZER, EnvironmentProviders, inject, InjectFlags, InjectionToken, Injector, makeEnvironmentProviders, Provider, Type} from '@angular/core';
import {of, Subject} from 'rxjs';
import {filter, map, take} from 'rxjs/operators';

Expand Down Expand Up @@ -59,16 +59,16 @@ const NG_DEV_MODE = typeof ngDevMode === 'undefined' || ngDevMode;
* @param features Optional features to configure additional router behaviors.
* @returns A set of providers to setup a Router.
*/
export function provideRouter(routes: Routes, ...features: RouterFeatures[]): Provider[] {
return [
export function provideRouter(routes: Routes, ...features: RouterFeatures[]): EnvironmentProviders {
return makeEnvironmentProviders([
provideRoutes(routes), {provide: ActivatedRoute, useFactory: rootRoute, deps: [Router]},
{provide: APP_BOOTSTRAP_LISTENER, multi: true, useFactory: getBootstrapListener},
features.map(feature => feature.ɵproviders),
// TODO: All options used by the `assignExtraOptionsToRouter` factory need to be reviewed for
// how we want them to be configured. This API doesn't currently have a way to configure them
// and we should decide what the _best_ way to do that is rather than just sticking with the
// status quo of how it's done today.
];
]);
}

export function rootRoute(router: Router): ActivatedRoute {
Expand Down
6 changes: 3 additions & 3 deletions packages/router/testing/src/provide_router_for_testing.ts
Expand Up @@ -8,7 +8,7 @@

import {Location, LocationStrategy} from '@angular/common';
import {MockLocationStrategy, SpyLocation} from '@angular/common/testing';
import {Provider} from '@angular/core';
import {EnvironmentProviders, Provider} from '@angular/core';
import {provideRouter, RouterFeatures, Routes} from '@angular/router';


Expand Down Expand Up @@ -46,9 +46,9 @@ import {provideRouter, RouterFeatures, Routes} from '@angular/router';
* @returns A set of providers to setup Router for testing.
*/
export function provideRouterForTesting(
routes: Routes = [], ...features: RouterFeatures[]): Provider[] {
routes: Routes = [], ...features: RouterFeatures[]): (Provider|EnvironmentProviders)[] {
return [
...provideRouter(routes, ...features),
provideRouter(routes, ...features),
{provide: Location, useClass: SpyLocation},
{provide: LocationStrategy, useClass: MockLocationStrategy},
];
Expand Down

0 comments on commit 07017a7

Please sign in to comment.