angular/packages
Andrew Scott 926c35f4ac docs: Deprecate class and InjectionToken and resolvers (#47924)
Class and `InjectionToken`-based guards and resolvers are not as configurable,
are less re-usable, require more boilerplate, cannot be defined inline with the route,
and require more in-depth knowledge of Angular features (`Injectable`/providers).
In short, they're less powerful and more cumbersome.

In addition, continued support increases the API surface which in turn increases
bundle size, code complexity, the learning curve and API surface to teach,
maintenance cost, and cognitive load (needing to grok several different types
of information in a single place).

Lastly, supporting only the `CanXFn` types for guards and `ResolveFn` type
for resolvers in the `Route` interface will enable better code
completion and integration with TypeScript. For example, when writing an
inline functional resolver today, the function is typed as `any` and
does not provide completions for the `ResolveFn` parameters. By
restricting the type to only `ResolveFn`, in the example below
TypeScript would be able to correctly identify the `route` parameter as
`ActivatedRouteSnapshot` and when authoring the inline route, the
language service would be able to autocomplete the function parameters.

```
const userRoute: Route = {
  path: 'user/:id',
  resolve: {
    "user": (route) => inject(UserService).getUser(route.params['id']);
  }
};
```

Importantly, this deprecation only affects the support for class and
`InjectionToken` guards at the `Route` definition. `Injectable` classes
and `InjectionToken` providers are _not_ being deprecated in the general
sense. Functional guards are robust enough to even support the existing
class-based guards through a transform:

```
function mapToCanMatch(providers: Array<Type<{canMatch: CanMatchFn}>>): CanMatchFn[] {
  return providers.map(provider => (...params) => inject(provider).canMatch(...params));
}
const route = {
  path: 'admin',
  canMatch: mapToCanMatch([AdminGuard]),
};
```

With regards to tests, because of the ability to map `Injectable`
classes to guard functions as outlined above, nothing _needs_ to change
if projects prefer testing guards the way they do today. Functional
guards can also be written in a way that's either testable with
`runInContext` or by passing mock implementations of dependencies.
For example:

```
export function myGuardWithMockableDeps(
  dep1 = inject(MyService),
  dep2 = inject(MyService2),
  dep3 = inject(MyService3),
) { }

const route = {
  path: 'admin',
  canActivate: [() => myGuardWithMockableDeps()]
}

// test file
const guardResultWithMockDeps = myGuardWithMockableDeps(mockService1, mockService2, mockService3);
const guardResultWithRealDeps = TestBed.inject(EnvironmentInjector).runInContext(myGuardWithMockableDeps);
```

DEPRECATED: Class and `InjectionToken` guards and resolvers are
deprecated. Instead, write guards as plain JavaScript functions and
inject dependencies with `inject` from `@angular/core`.

PR Close #47924
2023-01-11 15:01:08 -08:00
..
animations refactor: update animations tests to work with ESM output (#48521) 2022-12-19 19:50:42 +00:00
bazel refactor(bazel): disables the Tsickle decorator transform (#47934) 2023-01-10 07:58:39 -08:00
benchpress build: ensure bootstrap transitive runfiles are made available (#48521) 2022-12-19 19:50:41 +00:00
common refactor(common): remove non-null assertions (#48476) 2023-01-05 14:46:20 -08:00
compiler refactor(compiler): refactor the shadow css specs (#48443) 2023-01-11 14:55:52 -08:00
compiler-cli fix(compiler): resolve deprecation warning (#48652) 2023-01-10 08:13:25 -08:00
core refactor(core): cleanup type any (#48623) 2023-01-04 12:15:16 -08:00
docs docs: fix packages\docs\di\di.md (#46704) 2022-07-08 17:53:39 +00:00
elements refactor: update elements test to work with ESM outputs (#48521) 2022-12-19 19:50:43 +00:00
examples build: remove remaining usages of deprecated ts_devserver (#48521) 2022-12-19 19:50:44 +00:00
forms fix(forms): Form provider FormsModule.withConfig return a FormsModule (#48526) 2023-01-05 16:26:21 -08:00
language-service fix(language-service): expose package.json for vscode extension resolution (#48678) 2023-01-10 09:13:54 -08:00
localize fix(localize): add triple slash type reference on @angular/localize on `ng add (#48502) 2023-01-04 11:50:45 -08:00
misc/angular-in-memory-web-api build: update angular-in-memory-web-api to work with ESM (#48521) 2022-12-19 19:50:42 +00:00
platform-browser refactor(compiler): refactor the shadow css specs (#48443) 2023-01-11 14:55:52 -08:00
platform-browser-dynamic refactor: update platform-browser-dynamic to work with ESM output (#48521) 2022-12-19 19:50:43 +00:00
platform-server refactor: update platform-server tests to work with ESM (#48521) 2022-12-19 19:50:43 +00:00
private/testing build: bundle tests for karma web test suites (#48521) 2022-12-19 19:50:42 +00:00
router docs: Deprecate class and InjectionToken and resolvers (#47924) 2023-01-11 15:01:08 -08:00
service-worker refactor: update service-worker tests to work with ESM (#48521) 2022-12-19 19:50:44 +00:00
upgrade refactor: update upgrade package tests to work with ESM output (#48521) 2022-12-19 19:50:43 +00:00
zone.js build: remove duplicate license banners from FESM bundles (#48560) 2023-01-02 12:16:55 +00:00
BUILD.bazel build(bazel): list docs entrypoints under packages to simplify docgen 2022-11-22 13:51:16 -07:00
circular-deps-test.conf.js build: ignore type only imports/exports for circular dependency checks (#47017) 2022-08-18 12:06:09 -07:00
empty.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
goog.d.ts refactor(core): replace references to goog.LOCALE with goog.getLocale() (#43089) 2021-08-10 10:35:26 -07:00
license-banner.txt build: bump license year (#44590) 2022-01-04 12:05:25 -08:00
README.md
system.d.ts build: update license headers to reference Google LLC (#37205) 2020-05-26 14:26:58 -04:00
tsconfig-build.json refactor: adjust packages/core/test to work with ESM (#48521) 2022-12-19 19:50:42 +00:00
tsconfig-legacy-saucelabs.json refactor: adjust packages/core/test to work with ESM (#48521) 2022-12-19 19:50:42 +00:00
tsconfig-test.json build: switch devmode output to es2015 (#44505) 2022-01-05 23:20:20 +00:00
tsconfig-tsec-base.json build: update tsconfigs to use ES2020 as target and module (#43431) 2021-10-01 18:28:42 +00:00
tsconfig.json refactor: adjust packages/core/test to work with ESM (#48521) 2022-12-19 19:50:42 +00:00
tsec-exemption.json fix(core): hardening attribute and property binding rules for <iframe> elements (#47964) 2022-11-09 00:47:56 -08:00
types.d.ts build: update to jasmine 4.0 (#45558) 2022-04-11 16:25:28 +00:00

Angular

The sources for this package are in the main Angular repo. Please file issues and pull requests against that repo.

Usage information and reference details can be found in Angular documentation.

License: MIT