Skip to content

Commit ec25af5

Browse files
committed
fix(@schematics/angular): import UrlSegment instead of subPath in guard generator
When generating class-based `CanMatch` guards (`ng g guard --implements=CanMatch --no-functional`), the guard implementation template defines `canMatch(route: Route, segments: UrlSegment[])`. Previously, the schematic added `'subPath'` instead of `'UrlSegment'` to `@angular/router` named imports. Because `@angular/router` does not export `subPath`, this generated code with broken imports (`Module '"@angular/router"' has no exported member 'subPath'`) while leaving `UrlSegment` unimported (`Cannot find name 'UrlSegment'`). This change ensures `UrlSegment` is imported when generating `CanMatch` guards.
1 parent ecf8c08 commit ec25af5

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

packages/schematics/angular/guard/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default function (options: GuardOptions): Rule {
3434
const routerNamedImports: string[] = [...options.implements, 'MaybeAsync', 'GuardResult'];
3535

3636
if (options.implements.includes(GuardInterface.CanMatch)) {
37-
routerNamedImports.push('Route', 'subPath');
37+
routerNamedImports.push('Route', 'UrlSegment');
3838

3939
if (options.implements.length > 1) {
4040
routerNamedImports.push(...commonRouterNameImports);

packages/schematics/angular/guard/index_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ describe('Guard Schematic', () => {
165165
const options = { ...defaultOptions, implements: implementationOptions, functional: false };
166166
const tree = await schematicRunner.runSchematic('guard', options, appTree);
167167
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.ts');
168-
const expectedImports = `import { CanMatch, GuardResult, MaybeAsync, Route, subPath } from '@angular/router';`;
168+
const expectedImports = `import { CanMatch, GuardResult, MaybeAsync, Route, UrlSegment } from '@angular/router';`;
169169

170170
expect(fileString).toContain(expectedImports);
171171
});
@@ -198,7 +198,7 @@ describe('Guard Schematic', () => {
198198
const fileString = tree.readContent('/projects/bar/src/app/foo-guard.ts');
199199
const expectedImports =
200200
`import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, CanMatch, GuardResult, ` +
201-
`MaybeAsync, Route, RouterStateSnapshot, subPath } from '@angular/router';`;
201+
`MaybeAsync, Route, RouterStateSnapshot, UrlSegment } from '@angular/router';`;
202202

203203
expect(fileString).toContain(expectedImports);
204204
});

0 commit comments

Comments
 (0)