Skip to content

Commit 5a8b1bc

Browse files
committed
docs(router): add documentation for lifecycle hooks
Closes angular#3334
1 parent ac6227e commit 5a8b1bc

File tree

2 files changed

+125
-7
lines changed

2 files changed

+125
-7
lines changed

modules/angular2/src/router/interfaces.ts

Lines changed: 101 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,131 @@ var __ignore_me = global;
88

99

1010
/**
11-
* Defines route lifecycle method [onActivate]
11+
* Defines route lifecycle method [onActivate], which is called by the router at the end of a
12+
* successful route navigation.
13+
*
14+
* For a single component's navigation, only one of either [onActivate] or [onReuse] will be called,
15+
* depending on the result of [canReuse].
16+
*
17+
* If `onActivate` returns a promise, the route change will wait until the promise settles to
18+
* instantiate and activate child components.
19+
*
20+
* ## Example
21+
* ```
22+
* @Directive({
23+
* selector: 'my-cmp'
24+
* })
25+
* class MyCmp implements OnActivate {
26+
* onActivate(next, prev) {
27+
* this.log = 'Finished navigating from ' + prev.urlPath + ' to ' + next.urlPath;
28+
* }
29+
* }
30+
* ```
1231
*/
1332
export interface OnActivate {
1433
onActivate(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction): any;
1534
}
1635

1736
/**
18-
* Defines route lifecycle method [onReuse]
37+
* Defines route lifecycle method [onReuse], which is called by the router at the end of a
38+
* successful route navigation when [canReuse] is implemented and returns or resolves to true.
39+
*
40+
* For a single component's navigation, only one of either [onActivate] or [onReuse] will be called,
41+
* depending on the result of [canReuse].
42+
*
43+
* ## Example
44+
* ```
45+
* @Directive({
46+
* selector: 'my-cmp'
47+
* })
48+
* class MyCmp implements CanReuse, OnReuse {
49+
* canReuse() {
50+
* return true;
51+
* }
52+
*
53+
* onReuse(next, prev) {
54+
* this.params = next.params;
55+
* }
56+
* }
57+
* ```
1958
*/
2059
export interface OnReuse {
2160
onReuse(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction): any;
2261
}
2362

2463
/**
25-
* Defines route lifecycle method [onDeactivate]
64+
* Defines route lifecycle method [onDeactivate], which is called by the router before destroying
65+
* a component as part of a route change.
66+
*
67+
* If `onDeactivate` returns a promise, the route change will wait until the promise settles.
68+
*
69+
* ## Example
70+
* ```
71+
* @Directive({
72+
* selector: 'my-cmp'
73+
* })
74+
* class MyCmp implements CanReuse, OnReuse {
75+
* canReuse() {
76+
* return true;
77+
* }
78+
*
79+
* onReuse(next, prev) {
80+
* this.params = next.params;
81+
* }
82+
* }
83+
* ```
2684
*/
2785
export interface OnDeactivate {
2886
onDeactivate(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction): any;
2987
}
3088

3189
/**
32-
* Defines route lifecycle method [canReuse]
90+
* Defines route lifecycle method [canReuse], which is called by the router to determine whether a
91+
* component should be reused across routes, or whether to destroy and instantiate a new component.
92+
*
93+
* If `canReuse` returns or resolves to `true`, the component instance will be reused.
94+
*
95+
* If `canReuse` throws or rejects, the navigation will be cancelled.
96+
*
97+
* ## Example
98+
* ```
99+
* @Directive({
100+
* selector: 'my-cmp'
101+
* })
102+
* class MyCmp implements CanReuse, OnReuse {
103+
* canReuse(next, prev) {
104+
* return next.params.id == prev.params.id;
105+
* }
106+
*
107+
* onReuse(next, prev) {
108+
* this.id = next.params.id;
109+
* }
110+
* }
111+
* ```
33112
*/
34113
export interface CanReuse {
35114
canReuse(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction): any;
36115
}
37116

38117
/**
39-
* Defines route lifecycle method [canDeactivate]
118+
* Defines route lifecycle method [canDeactivate], which is called by the router to determine
119+
* if a component can be removed as part of a navigation.
120+
*
121+
* If `canDeactivate` returns or resolves to `false`, the navigation is cancelled.
122+
*
123+
* If `canDeactivate` throws or rejects, the navigation is also cancelled.
124+
*
125+
* ## Example
126+
* ```
127+
* @Directive({
128+
* selector: 'my-cmp'
129+
* })
130+
* class MyCmp implements CanDeactivate {
131+
* canDeactivate(next, prev) {
132+
* return askUserIfTheyAreSureTheyWantToQuit();
133+
* }
134+
* }
135+
* ```
40136
*/
41137
export interface CanDeactivate {
42138
canDeactivate(nextInstruction: ComponentInstruction, prevInstruction: ComponentInstruction): any;

modules/angular2/src/router/lifecycle_annotations.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,28 @@ export {
1616
onDeactivate
1717
} from './lifecycle_annotations_impl';
1818

19+
/**
20+
* Defines route lifecycle method [canActivate], which is called by the router to determine
21+
* if a component can be instantiated as part of a navigation.
22+
*
23+
* Note that unlike other lifecycle hooks, this one uses an annotation rather than an interface.
24+
* This is because [canActivate] is called before the component is instantiated.
25+
*
26+
* If `canActivate` returns or resolves to `false`, the navigation is cancelled.
27+
*
28+
* If `canActivate` throws or rejects, the navigation is also cancelled.
29+
*
30+
* ## Example
31+
* ```
32+
* @Directive({
33+
* selector: 'control-panel-cmp'
34+
* })
35+
* @CanActivate(() => checkIfUserIsLoggedIn())
36+
* class ControlPanelCmp {
37+
* // ...
38+
* }
39+
* ```
40+
*/
1941
export var CanActivate:
20-
(hook: (next: ComponentInstruction, prev: ComponentInstruction) => Promise<boolean>| boolean) => ClassDecorator =
21-
makeDecorator(CanActivateAnnotation);
42+
(hook: (next: ComponentInstruction, prev: ComponentInstruction) => Promise<boolean>| boolean) =>
43+
ClassDecorator = makeDecorator(CanActivateAnnotation);

0 commit comments

Comments
 (0)