Commit 926c35f
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 #479241 parent 7f68a70 commit 926c35f
File tree
5 files changed
+61
-26
lines changed- aio/content/guide
- goldens/public-api/router
- packages/router/src
- operators
5 files changed
+61
-26
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
| 120 | + | |
120 | 121 | | |
121 | 122 | | |
122 | 123 | | |
| |||
203 | 204 | | |
204 | 205 | | |
205 | 206 | | |
| 207 | + | |
| 208 | + | |
206 | 209 | | |
207 | 210 | | |
208 | 211 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
111 | 112 | | |
112 | 113 | | |
113 | 114 | | |
114 | | - | |
| 115 | + | |
115 | 116 | | |
116 | 117 | | |
117 | 118 | | |
118 | 119 | | |
119 | 120 | | |
120 | | - | |
| 121 | + | |
121 | 122 | | |
122 | 123 | | |
123 | 124 | | |
| |||
129 | 130 | | |
130 | 131 | | |
131 | 132 | | |
132 | | - | |
| 133 | + | |
133 | 134 | | |
134 | 135 | | |
135 | 136 | | |
| |||
147 | 148 | | |
148 | 149 | | |
149 | 150 | | |
150 | | - | |
| 151 | + | |
151 | 152 | | |
152 | 153 | | |
153 | 154 | | |
| |||
237 | 238 | | |
238 | 239 | | |
239 | 240 | | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
240 | 244 | | |
241 | 245 | | |
242 | 246 | | |
| |||
561 | 565 | | |
562 | 566 | | |
563 | 567 | | |
564 | | - | |
| 568 | + | |
565 | 569 | | |
566 | 570 | | |
567 | 571 | | |
568 | 572 | | |
569 | 573 | | |
570 | 574 | | |
571 | 575 | | |
572 | | - | |
| 576 | + | |
573 | 577 | | |
574 | 578 | | |
575 | 579 | | |
| |||
611 | 615 | | |
612 | 616 | | |
613 | 617 | | |
614 | | - | |
615 | | - | |
616 | | - | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
617 | 621 | | |
618 | | - | |
619 | | - | |
| 622 | + | |
| 623 | + | |
620 | 624 | | |
621 | 625 | | |
622 | 626 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
41 | 58 | | |
42 | 59 | | |
43 | 60 | | |
| |||
110 | 127 | | |
111 | 128 | | |
112 | 129 | | |
113 | | - | |
| 130 | + | |
114 | 131 | | |
115 | 132 | | |
116 | 133 | | |
| |||
517 | 534 | | |
518 | 535 | | |
519 | 536 | | |
520 | | - | |
| 537 | + | |
521 | 538 | | |
522 | 539 | | |
523 | 540 | | |
| |||
526 | 543 | | |
527 | 544 | | |
528 | 545 | | |
529 | | - | |
| 546 | + | |
530 | 547 | | |
531 | 548 | | |
532 | 549 | | |
| |||
535 | 552 | | |
536 | 553 | | |
537 | 554 | | |
538 | | - | |
| 555 | + | |
539 | 556 | | |
540 | 557 | | |
541 | 558 | | |
| |||
544 | 561 | | |
545 | 562 | | |
546 | 563 | | |
547 | | - | |
| 564 | + | |
548 | 565 | | |
549 | 566 | | |
550 | 567 | | |
| |||
554 | 571 | | |
555 | 572 | | |
556 | 573 | | |
557 | | - | |
| 574 | + | |
558 | 575 | | |
559 | 576 | | |
560 | 577 | | |
| |||
696 | 713 | | |
697 | 714 | | |
698 | 715 | | |
| 716 | + | |
| 717 | + | |
699 | 718 | | |
700 | 719 | | |
701 | 720 | | |
| |||
791 | 810 | | |
792 | 811 | | |
793 | 812 | | |
| 813 | + | |
| 814 | + | |
794 | 815 | | |
795 | 816 | | |
796 | 817 | | |
| |||
880 | 901 | | |
881 | 902 | | |
882 | 903 | | |
| 904 | + | |
| 905 | + | |
883 | 906 | | |
884 | 907 | | |
885 | 908 | | |
| |||
982 | 1005 | | |
983 | 1006 | | |
984 | 1007 | | |
| 1008 | + | |
| 1009 | + | |
985 | 1010 | | |
986 | 1011 | | |
987 | 1012 | | |
| |||
1112 | 1137 | | |
1113 | 1138 | | |
1114 | 1139 | | |
| 1140 | + | |
| 1141 | + | |
1115 | 1142 | | |
1116 | 1143 | | |
1117 | 1144 | | |
| |||
1196 | 1223 | | |
1197 | 1224 | | |
1198 | 1225 | | |
1199 | | - | |
| 1226 | + | |
1200 | 1227 | | |
1201 | 1228 | | |
1202 | 1229 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
142 | 142 | | |
143 | 143 | | |
144 | 144 | | |
145 | | - | |
| 145 | + | |
| 146 | + | |
146 | 147 | | |
147 | 148 | | |
148 | 149 | | |
| |||
161 | 162 | | |
162 | 163 | | |
163 | 164 | | |
164 | | - | |
165 | | - | |
| 165 | + | |
| 166 | + | |
166 | 167 | | |
167 | 168 | | |
168 | 169 | | |
| |||
180 | 181 | | |
181 | 182 | | |
182 | 183 | | |
183 | | - | |
| 184 | + | |
184 | 185 | | |
185 | 186 | | |
186 | 187 | | |
| |||
213 | 214 | | |
214 | 215 | | |
215 | 216 | | |
216 | | - | |
| 217 | + | |
217 | 218 | | |
218 | 219 | | |
219 | 220 | | |
| |||
0 commit comments