Skip to content

Commit 69fa3bb

Browse files
Dzmitry Shylovichhansl
authored andcommitted
feat(router): add an extra argument to CanDeactivate interface (#13560)
Adds a `nextState` argument to access the future url from `CanDeactivate`. BEFORE: canDeactivate(component: T, route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean>|Promise<boolean>|boolean; AFTER: canDeactivate(component: T, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot, nextState?: RouterStateSnapshot): Observable<boolean>|Promise<boolean>|boolean; Closes #9853
1 parent 445ed43 commit 69fa3bb

4 files changed

Lines changed: 75 additions & 8 deletions

File tree

‎modules/@angular/router/src/interfaces.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,9 @@ export interface CanActivateChild {
185185
*
186186
* canDeactivate(
187187
* component: TeamComponent,
188-
* route: ActivatedRouteSnapshot,
189-
* state: RouterStateSnapshot
188+
* currentRoute: ActivatedRouteSnapshot,
189+
* currentState: RouterStateSnapshot,
190+
* nextState: RouterStateSnapshot
190191
* ): Observable<boolean>|Promise<boolean>|boolean {
191192
* return this.permissions.canDeactivate(this.currentUser, route.params.id);
192193
* }
@@ -223,7 +224,8 @@ export interface CanActivateChild {
223224
* providers: [
224225
* {
225226
* provide: 'canDeactivateTeam',
226-
* useValue: (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => true
227+
* useValue: (component: TeamComponent, currentRoute: ActivatedRouteSnapshot, currentState:
228+
* RouterStateSnapshot, nextState: RouterStateSnapshot) => true
227229
* }
228230
* ]
229231
* })
@@ -233,8 +235,9 @@ export interface CanActivateChild {
233235
* @stable
234236
*/
235237
export interface CanDeactivate<T> {
236-
canDeactivate(component: T, route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
237-
Observable<boolean>|Promise<boolean>|boolean;
238+
canDeactivate(
239+
component: T, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot,
240+
nextState?: RouterStateSnapshot): Observable<boolean>|Promise<boolean>|boolean;
238241
}
239242

240243
/**

‎modules/@angular/router/src/router.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -985,9 +985,10 @@ export class PreActivation {
985985
const guard = this.getToken(c, curr);
986986
let observable: Observable<boolean>;
987987
if (guard.canDeactivate) {
988-
observable = wrapIntoObservable(guard.canDeactivate(component, curr, this.curr));
988+
observable =
989+
wrapIntoObservable(guard.canDeactivate(component, curr, this.curr, this.future));
989990
} else {
990-
observable = wrapIntoObservable(guard(component, curr, this.curr));
991+
observable = wrapIntoObservable(guard(component, curr, this.curr, this.future));
991992
}
992993
return first.call(observable);
993994
});

‎modules/@angular/router/test/integration.spec.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,69 @@ describe('Integration', () => {
16471647
expect(location.path()).toEqual('/simple');
16481648
})));
16491649

1650+
describe('next state', () => {
1651+
let log: string[];
1652+
1653+
class ClassWithNextState implements CanDeactivate<TeamCmp> {
1654+
canDeactivate(
1655+
component: TeamCmp, currentRoute: ActivatedRouteSnapshot,
1656+
currentState: RouterStateSnapshot, nextState: RouterStateSnapshot): boolean {
1657+
log.push(currentState.url, nextState.url);
1658+
return true;
1659+
}
1660+
}
1661+
1662+
beforeEach(() => {
1663+
log = [];
1664+
TestBed.configureTestingModule({
1665+
providers: [
1666+
ClassWithNextState, {
1667+
provide: 'FunctionWithNextState',
1668+
useValue: (cmp: any, currentRoute: ActivatedRouteSnapshot,
1669+
currentState: RouterStateSnapshot, nextState: RouterStateSnapshot) => {
1670+
log.push(currentState.url, nextState.url);
1671+
return true;
1672+
}
1673+
}
1674+
]
1675+
});
1676+
});
1677+
1678+
it('should pass next state as the 4 argument when guard is a class',
1679+
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
1680+
const fixture = createRoot(router, RootCmp);
1681+
1682+
router.resetConfig(
1683+
[{path: 'team/:id', component: TeamCmp, canDeactivate: [ClassWithNextState]}]);
1684+
1685+
router.navigateByUrl('/team/22');
1686+
advance(fixture);
1687+
expect(location.path()).toEqual('/team/22');
1688+
1689+
router.navigateByUrl('/team/33');
1690+
advance(fixture);
1691+
expect(location.path()).toEqual('/team/33');
1692+
expect(log).toEqual(['/team/22', '/team/33']);
1693+
})));
1694+
1695+
it('should pass next state as the 4 argument when guard is a function',
1696+
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
1697+
const fixture = createRoot(router, RootCmp);
1698+
1699+
router.resetConfig([
1700+
{path: 'team/:id', component: TeamCmp, canDeactivate: ['FunctionWithNextState']}
1701+
]);
1702+
1703+
router.navigateByUrl('/team/22');
1704+
advance(fixture);
1705+
expect(location.path()).toEqual('/team/22');
1706+
1707+
router.navigateByUrl('/team/33');
1708+
advance(fixture);
1709+
expect(location.path()).toEqual('/team/33');
1710+
expect(log).toEqual(['/team/22', '/team/33']);
1711+
})));
1712+
});
16501713

16511714
describe('should work when given a class', () => {
16521715
class AlwaysTrue implements CanDeactivate<TeamCmp> {

‎tools/public_api_guard/router/index.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export interface CanActivateChild {
4747

4848
/** @stable */
4949
export interface CanDeactivate<T> {
50-
canDeactivate(component: T, route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean;
50+
canDeactivate(component: T, currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot, nextState?: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean;
5151
}
5252

5353
/** @stable */

0 commit comments

Comments
 (0)