Skip to content

Commit eef2c19

Browse files
Broccohansl
authored andcommitted
fix(@schematics/angular): Correct routing scope for new projects
fixes #7524
1 parent 4a64f38 commit eef2c19

5 files changed

Lines changed: 24 additions & 3 deletions

File tree

‎packages/schematics/angular/application/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export default function (options: ApplicationOptions): Rule {
105105
commonModule: false,
106106
flat: true,
107107
routing: options.routing,
108+
routingScope: 'Root',
108109
sourceDir: options.directory + '/' + options.sourceDir,
109110
spec: false,
110111
}),

‎packages/schematics/angular/application/index_spec.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { SchematicTestRunner } from '@angular-devkit/schematics/test';
9+
import { getFileContent } from '../utility/test';
910
import { Schema as AppSchema } from './schema';
1011

1112

@@ -63,4 +64,16 @@ describe('Application Schematic', () => {
6364
expect(files.indexOf('/foo/src/app/app.component.ts')).toBeGreaterThanOrEqual(0);
6465
});
6566

67+
it('should handle the routing flag', () => {
68+
const options = { ...defaultOptions, routing: true };
69+
70+
const tree = schematicRunner.runSchematic('application', options);
71+
const files = tree.files;
72+
expect(files.indexOf('/foo/src/app/app.module.ts')).toBeGreaterThanOrEqual(0);
73+
expect(files.indexOf('/foo/src/app/app-routing.module.ts')).toBeGreaterThanOrEqual(0);
74+
const moduleContent = getFileContent(tree, '/foo/src/app/app.module.ts');
75+
expect(moduleContent).toMatch(/import { AppRoutingModule } from '.\/app-routing.module'/);
76+
const routingModuleContent = getFileContent(tree, '/foo/src/app/app-routing.module.ts');
77+
expect(routingModuleContent).toMatch(/RouterModule.forRoot\(routes\)/);
78+
});
6679
});

‎packages/schematics/angular/module/files/__path__/__name@dasherize@if-flat__/__name@dasherize__-routing.module.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Routes, RouterModule } from '@angular/router';
44
const routes: Routes = [];
55

66
@NgModule({
7-
imports: [RouterModule.forChild(routes)],
7+
imports: [RouterModule.for<%= routingScope %>(routes)],
88
exports: [RouterModule]
99
})
1010
export class <%= classify(name) %>RoutingModule { }

‎packages/schematics/angular/module/index_spec.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ describe('Module Schematic', () => {
5454
const files = tree.files;
5555
expect(files.indexOf('/src/app/foo/foo.module.ts')).toBeGreaterThanOrEqual(0);
5656
expect(files.indexOf('/src/app/foo/foo-routing.module.ts')).toBeGreaterThanOrEqual(0);
57+
const moduleContent = getFileContent(tree, '/src/app/foo/foo.module.ts');
58+
expect(moduleContent).toMatch(/import { FooRoutingModule } from '.\/foo-routing.module'/);
59+
const routingModuleContent = getFileContent(tree, '/src/app/foo/foo-routing.module.ts');
60+
expect(routingModuleContent).toMatch(/RouterModule.forChild\(routes\)/);
5761
});
5862

5963
it('should respect the spec flag', () => {
@@ -73,6 +77,4 @@ describe('Module Schematic', () => {
7377
expect(files.indexOf('/src/app/two-word/two-word.module.ts')).toBeGreaterThanOrEqual(0);
7478
expect(files.indexOf('/src/app/two-word/two-word.module.spec.ts')).toBeGreaterThanOrEqual(0);
7579
});
76-
77-
7880
});

‎packages/schematics/angular/module/schema.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
"type": "boolean",
2020
"default": false
2121
},
22+
"routingScope": {
23+
"enum": ["Child", "Root"],
24+
"type": "string",
25+
"default": "Child"
26+
},
2227
"spec": {
2328
"type": "boolean",
2429
"default": true

0 commit comments

Comments
 (0)