Skip to content

Commit eae7a27

Browse files
cexbrayatdgp1130
authored andcommitted
fix(@schematics/angular): add CanDeactivate guard
This generates a generic `CanDeactivate<unknown>` guard. Fixes #15668 (cherry picked from commit 2fc3fab)
1 parent 721b553 commit eae7a27

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

packages/schematics/angular/guard/files/__name@dasherize__.guard.ts.template

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ export class <%= classify(name) %>Guard implements <%= implementations %> {
1616
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
1717
return true;
1818
}
19+
<% } %><% if (implements.includes('CanDeactivate')) { %>canDeactivate(
20+
component: unknown,
21+
currentRoute: ActivatedRouteSnapshot,
22+
currentState: RouterStateSnapshot,
23+
nextState?: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
24+
return true;
25+
}
1926
<% } %><% if (implements.includes('CanLoad')) { %>canLoad(
2027
route: Route,
2128
segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean {

packages/schematics/angular/guard/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ export default function (options: GuardOptions): Rule {
3535
throw new SchematicsException('Option "implements" is required.');
3636
}
3737

38-
const implementations = options.implements.join(', ');
39-
let implementationImports = `${implementations}, `;
38+
const implementations = options.implements
39+
.map(implement => implement === 'CanDeactivate' ? 'CanDeactivate<unknown>' : implement)
40+
.join(', ');
41+
let implementationImports = `${options.implements.join(', ')}, `;
4042
// As long as we aren't in IE... ;)
4143
if (options.implements.includes(GuardInterface.CanLoad)) {
4244
implementationImports = `${implementationImports}Route, UrlSegment, `;

packages/schematics/angular/guard/schema.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"enum": [
6060
"CanActivate",
6161
"CanActivateChild",
62+
"CanDeactivate",
6263
"CanLoad"
6364
],
6465
"type": "string"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {join} from 'path';
2+
import {ng} from '../../../utils/process';
3+
import {expectFileToExist,expectFileToMatch} from '../../../utils/fs';
4+
5+
6+
export default async function() {
7+
// Does not create a sub directory.
8+
const guardDir = join('src', 'app');
9+
10+
await ng('generate', 'guard', 'load', '--implements=CanLoad', '--implements=CanDeactivate');
11+
await expectFileToExist(guardDir);
12+
await expectFileToExist(join(guardDir, 'load.guard.ts'));
13+
await expectFileToMatch(join(guardDir, 'load.guard.ts'), /implements CanLoad, CanDeactivate<unknown>/);
14+
await expectFileToExist(join(guardDir, 'load.guard.spec.ts'));
15+
await ng('test', '--watch=false');
16+
}

0 commit comments

Comments
 (0)