diff --git a/packages/schematics/angular/guard/files/__name@dasherize__.guard.ts.template b/packages/schematics/angular/guard/files/__name@dasherize__.guard.ts.template index 3dc36a017893..8d83bc7498b4 100644 --- a/packages/schematics/angular/guard/files/__name@dasherize__.guard.ts.template +++ b/packages/schematics/angular/guard/files/__name@dasherize__.guard.ts.template @@ -23,7 +23,11 @@ export class <%= classify(name) %>Guard implements <%= implementations %> { nextState?: RouterStateSnapshot): Observable | Promise | boolean | UrlTree { return true; } - <% } %><% if (implements.includes('CanLoad')) { %>canLoad( + <% } %><% if (implements.includes('CanMatch')) { %>canMatch( + route: Route, + segments: UrlSegment[]): Observable | Promise | boolean | UrlTree { + return true; + }<% } %><% if (implements.includes('CanLoad')) { %>canLoad( route: Route, segments: UrlSegment[]): Observable | Promise | boolean | UrlTree { return true; diff --git a/packages/schematics/angular/guard/index.ts b/packages/schematics/angular/guard/index.ts index f8a35b9947bf..efb377216684 100644 --- a/packages/schematics/angular/guard/index.ts +++ b/packages/schematics/angular/guard/index.ts @@ -21,7 +21,10 @@ export default function (options: GuardOptions): Rule { const commonRouterNameImports = ['ActivatedRouteSnapshot', 'RouterStateSnapshot']; const routerNamedImports: string[] = [...options.implements, 'UrlTree']; - if (options.implements.includes(GuardInterface.CanLoad)) { + if ( + options.implements.includes(GuardInterface.CanLoad) || + options.implements.includes(GuardInterface.CanMatch) + ) { routerNamedImports.push('Route', 'UrlSegment'); if (options.implements.length > 1) { diff --git a/packages/schematics/angular/guard/index_spec.ts b/packages/schematics/angular/guard/index_spec.ts index eba8e654982b..45326eba1862 100644 --- a/packages/schematics/angular/guard/index_spec.ts +++ b/packages/schematics/angular/guard/index_spec.ts @@ -126,6 +126,16 @@ describe('Guard Schematic', () => { expect(fileString).toContain(expectedImports); }); + it('should add correct imports based on CanMatch implementation', async () => { + const implementationOptions = ['CanMatch']; + const options = { ...defaultOptions, implements: implementationOptions }; + const tree = await schematicRunner.runSchematicAsync('guard', options, appTree).toPromise(); + const fileString = tree.readContent('/projects/bar/src/app/foo.guard.ts'); + const expectedImports = `import { CanMatch, Route, UrlSegment, UrlTree } from '@angular/router';`; + + expect(fileString).toContain(expectedImports); + }); + it('should add correct imports based on CanActivate implementation', async () => { const implementationOptions = ['CanActivate']; const options = { ...defaultOptions, implements: implementationOptions }; diff --git a/packages/schematics/angular/guard/schema.json b/packages/schematics/angular/guard/schema.json index f66bdc9428cc..fda5ea7a43a2 100644 --- a/packages/schematics/angular/guard/schema.json +++ b/packages/schematics/angular/guard/schema.json @@ -48,7 +48,7 @@ "uniqueItems": true, "minItems": 1, "items": { - "enum": ["CanActivate", "CanActivateChild", "CanDeactivate", "CanLoad"], + "enum": ["CanActivate", "CanActivateChild", "CanDeactivate", "CanLoad", "CanMatch"], "type": "string" }, "default": ["CanActivate"],