Skip to content

Commit b40d3c0

Browse files
jbogarthydeAndrewKushnir
authored andcommitted
docs: update reference doc for router guards and resolvers (angular#38079)
Complete and clarify descriptions and example of the guard and resolver functions in Router API documentation. PR Close angular#38079
1 parent 0e56171 commit b40d3c0

File tree

1 file changed

+86
-23
lines changed

1 file changed

+86
-23
lines changed

packages/router/src/interfaces.ts

Lines changed: 86 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ import {UrlSegment, UrlTree} from './url_tree';
1717
* @description
1818
*
1919
* Interface that a class can implement to be a guard deciding if a route can be activated.
20-
* If all guards return `true`, navigation will continue. If any guard returns `false`,
21-
* navigation will be cancelled. If any guard returns a `UrlTree`, current navigation will
22-
* be cancelled and a new navigation will be kicked off to the `UrlTree` returned from the
23-
* guard.
20+
* If all guards return `true`, navigation continues. If any guard returns `false`,
21+
* navigation is cancelled. If any guard returns a `UrlTree`, the current navigation
22+
* is cancelled and a new navigation begins to the `UrlTree` returned from the guard.
23+
*
24+
* The following example implements a `CanActivate` function that checks whether the
25+
* current user has permission to activate the requested route.
2426
*
2527
* ```
2628
* class UserToken {}
@@ -41,7 +43,12 @@ import {UrlSegment, UrlTree} from './url_tree';
4143
* return this.permissions.canActivate(this.currentUser, route.params.id);
4244
* }
4345
* }
46+
* ```
4447
*
48+
* Here, the defined guard function is provided as part of the `Route` object
49+
* in the router configuration:
50+
*
51+
* ```
4552
* @NgModule({
4653
* imports: [
4754
* RouterModule.forRoot([
@@ -57,7 +64,7 @@ import {UrlSegment, UrlTree} from './url_tree';
5764
* class AppModule {}
5865
* ```
5966
*
60-
* You can alternatively provide a function with the `canActivate` signature:
67+
* You can alternatively provide an in-line function with the `canActivate` signature:
6168
*
6269
* ```
6370
* @NgModule({
@@ -94,10 +101,12 @@ export type CanActivateFn = (route: ActivatedRouteSnapshot, state: RouterStateSn
94101
* @description
95102
*
96103
* Interface that a class can implement to be a guard deciding if a child route can be activated.
97-
* If all guards return `true`, navigation will continue. If any guard returns `false`,
98-
* navigation will be cancelled. If any guard returns a `UrlTree`, current navigation will
99-
* be cancelled and a new navigation will be kicked off to the `UrlTree` returned from the
100-
* guard.
104+
* If all guards return `true`, navigation continues. If any guard returns `false`,
105+
* navigation is cancelled. If any guard returns a `UrlTree`, current navigation
106+
* is cancelled and a new navigation begins to the `UrlTree` returned from the guard.
107+
*
108+
* The following example implements a `CanActivateChild` function that checks whether the
109+
* current user has permission to activate the requested child route.
101110
*
102111
* ```
103112
* class UserToken {}
@@ -118,7 +127,12 @@ export type CanActivateFn = (route: ActivatedRouteSnapshot, state: RouterStateSn
118127
* return this.permissions.canActivate(this.currentUser, route.params.id);
119128
* }
120129
* }
130+
* ```
121131
*
132+
* Here, the defined guard function is provided as part of the `Route` object
133+
* in the router configuration:
134+
*
135+
* ```
122136
* @NgModule({
123137
* imports: [
124138
* RouterModule.forRoot([
@@ -139,7 +153,7 @@ export type CanActivateFn = (route: ActivatedRouteSnapshot, state: RouterStateSn
139153
* class AppModule {}
140154
* ```
141155
*
142-
* You can alternatively provide a function with the `canActivateChild` signature:
156+
* You can alternatively provide an in-line function with the `canActivateChild` signature:
143157
*
144158
* ```
145159
* @NgModule({
@@ -181,10 +195,12 @@ export type CanActivateChildFn = (childRoute: ActivatedRouteSnapshot, state: Rou
181195
* @description
182196
*
183197
* Interface that a class can implement to be a guard deciding if a route can be deactivated.
184-
* If all guards return `true`, navigation will continue. If any guard returns `false`,
185-
* navigation will be cancelled. If any guard returns a `UrlTree`, current navigation will
186-
* be cancelled and a new navigation will be kicked off to the `UrlTree` returned from the
187-
* guard.
198+
* If all guards return `true`, navigation continues. If any guard returns `false`,
199+
* navigation is cancelled. If any guard returns a `UrlTree`, current navigation
200+
* is cancelled and a new navigation begins to the `UrlTree` returned from the guard.
201+
*
202+
* The following example implements a `CanDeactivate` function that checks whether the
203+
* current user has permission to deactivate the requested route.
188204
*
189205
* ```
190206
* class UserToken {}
@@ -193,6 +209,12 @@ export type CanActivateChildFn = (childRoute: ActivatedRouteSnapshot, state: Rou
193209
* return true;
194210
* }
195211
* }
212+
* ```
213+
*
214+
* Here, the defined guard function is provided as part of the `Route` object
215+
* in the router configuration:
216+
*
217+
* ```
196218
*
197219
* @Injectable()
198220
* class CanDeactivateTeam implements CanDeactivate<TeamComponent> {
@@ -223,7 +245,7 @@ export type CanActivateChildFn = (childRoute: ActivatedRouteSnapshot, state: Rou
223245
* class AppModule {}
224246
* ```
225247
*
226-
* You can alternatively provide a function with the `canDeactivate` signature:
248+
* You can alternatively provide an in-line function with the `canDeactivate` signature:
227249
*
228250
* ```
229251
* @NgModule({
@@ -266,8 +288,11 @@ export type CanDeactivateFn<T> =
266288
*
267289
* Interface that classes can implement to be a data provider.
268290
* A data provider class can be used with the router to resolve data during navigation.
269-
* The interface defines a `resolve()` method that will be invoked when the navigation starts.
270-
* The router will then wait for the data to be resolved before the route is finally activated.
291+
* The interface defines a `resolve()` method that is invoked when the navigation starts.
292+
* The router waits for the data to be resolved before the route is finally activated.
293+
*
294+
* The following example implements a `resolve()` method that retrieves the data
295+
* needed to activate the requested route.
271296
*
272297
* ```
273298
* @Injectable({ providedIn: 'root' })
@@ -281,7 +306,13 @@ export type CanDeactivateFn<T> =
281306
* return this.service.getHero(route.paramMap.get('id'));
282307
* }
283308
* }
309+
* ```
284310
*
311+
* Here, the defined `resolve()` function is provided as part of the `Route` object
312+
* in the router configuration:
313+
*
314+
* ```
315+
285316
* @NgModule({
286317
* imports: [
287318
* RouterModule.forRoot([
@@ -299,7 +330,7 @@ export type CanDeactivateFn<T> =
299330
* export class AppRoutingModule {}
300331
* ```
301332
*
302-
* You can alternatively provide a function with the `resolve` signature:
333+
* You can alternatively provide an in-line function with the `resolve()` signature:
303334
*
304335
* ```
305336
* export const myHero: Hero = {
@@ -328,6 +359,29 @@ export type CanDeactivateFn<T> =
328359
* export class AppModule {}
329360
* ```
330361
*
362+
* @usageNotes
363+
*
364+
* When both guard and resolvers are specified, the resolvers are not executed until
365+
* all guards have run and succeeded.
366+
* For example, consider the following route configuration:
367+
*
368+
* ```
369+
* {
370+
* path: 'base'
371+
* canActivate: [BaseGuard],
372+
* resolve: {data: BaseDataResolver}
373+
* children: [
374+
* {
375+
* path: 'child',
376+
* guards: [ChildGuard],
377+
* component: ChildComponent,
378+
* resolve: {childData: ChildDataResolver}
379+
* }
380+
* ]
381+
* }
382+
* ```
383+
* The order of execution is: BaseGuard, ChildGuard, BaseDataResolver, ChildDataResolver.
384+
*
331385
* @publicApi
332386
*/
333387
export interface Resolve<T> {
@@ -339,10 +393,13 @@ export interface Resolve<T> {
339393
* @description
340394
*
341395
* Interface that a class can implement to be a guard deciding if children can be loaded.
342-
* If all guards return `true`, navigation will continue. If any guard returns `false`,
343-
* navigation will be cancelled. If any guard returns a `UrlTree`, current navigation will
344-
* be cancelled and a new navigation will be kicked off to the `UrlTree` returned from the
345-
* guard.
396+
* If all guards return `true`, navigation continues. If any guard returns `false`,
397+
* navigation is cancelled. If any guard returns a `UrlTree`, current navigation
398+
* is cancelled and a new navigation starts to the `UrlTree` returned from the guard.
399+
*
400+
* The following example implements a `CanLoad` function that decides whether the
401+
* current user has permission to load requested child routes.
402+
*
346403
*
347404
* ```
348405
* class UserToken {}
@@ -360,6 +417,12 @@ export interface Resolve<T> {
360417
* return this.permissions.canLoadChildren(this.currentUser, route, segments);
361418
* }
362419
* }
420+
* ```
421+
*
422+
* Here, the defined guard function is provided as part of the `Route` object
423+
* in the router configuration:
424+
*
425+
* ```
363426
*
364427
* @NgModule({
365428
* imports: [
@@ -377,7 +440,7 @@ export interface Resolve<T> {
377440
* class AppModule {}
378441
* ```
379442
*
380-
* You can alternatively provide a function with the `canLoad` signature:
443+
* You can alternatively provide an in-line function with the `canLoad` signature:
381444
*
382445
* ```
383446
* @NgModule({

0 commit comments

Comments
 (0)