Skip to content

Commit a279248

Browse files
committed
fix(@schematics/angular): prevent importing RouterModule parallel to RoutingModule
Prior to this commit, we defined routes in two places example: ```ts @NgModule({ declarations: [ HomeComponent ], imports: [ CommonModule, RouterModule.forChild(routes), HomeRoutingModule ] }) export class HomeModule { } ``` Closes angular#17139
1 parent d6b9306 commit a279248

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

packages/schematics/angular/module/files/__name@dasherize@if-flat__/__name@dasherize__.module.ts.template

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NgModule } from '@angular/core';<% if (commonModule) { %>
22
import { CommonModule } from '@angular/common';<% } %><% if (lazyRouteWithoutRouteModule) { %>
33
import { Routes, RouterModule } from '@angular/router';<% } %>
4-
<% if (routing || lazyRouteWithRouteModule) { %>
4+
<% if ((!lazyRoute && routing) || lazyRouteWithRouteModule) { %>
55
import { <%= classify(name) %>RoutingModule } from './<%= dasherize(name) %>-routing.module';<% } %>
66
<% if (lazyRouteWithoutRouteModule) { %>
77
const routes: Routes = [
@@ -11,7 +11,7 @@ const routes: Routes = [
1111
@NgModule({
1212
declarations: [],
1313
imports: [<% if (commonModule) { %>
14-
CommonModule<% } %><% if (routing || lazyRouteWithRouteModule) { %>,
14+
CommonModule<% } %><% if ((!lazyRoute && routing) || lazyRouteWithRouteModule) { %>,
1515
<%= classify(name) %>RoutingModule<% } %><% if (lazyRouteWithoutRouteModule) { %>,
1616
RouterModule.forChild(routes)<% } %>
1717
]

packages/schematics/angular/module/index_spec.ts

+41
Original file line numberDiff line numberDiff line change
@@ -288,5 +288,46 @@ describe('Module Schematic', () => {
288288
`loadChildren: () => import('../bar/bar.module').then(m => m.BarModule)`,
289289
);
290290
});
291+
292+
it('should not add reference to and RouterModule when referencing lazy routing module', async () => {
293+
// Delete routing module
294+
appTree.delete('/projects/bar/src/app/app-routing.module.ts');
295+
296+
// Update app.module to contain the route config.
297+
appTree.overwrite(
298+
'projects/bar/src/app/app.module.ts',
299+
`
300+
import { NgModule } from '@angular/core';
301+
import { RouterModule } from '@angular/router';
302+
import { BrowserModule } from '@angular/platform-browser';
303+
import { AppComponent } from './app.component';
304+
305+
306+
@NgModule({
307+
imports: [BrowserModule, RouterModule.forRoot([])],
308+
declarations: [AppComponent],
309+
})
310+
export class AppModule {}
311+
`,
312+
);
313+
314+
const tree = await schematicRunner
315+
.runSchematicAsync(
316+
'module',
317+
{
318+
...defaultOptions,
319+
name: 'bar',
320+
route: 'bar',
321+
routing: true,
322+
module: 'app.module.ts',
323+
},
324+
appTree,
325+
)
326+
.toPromise();
327+
328+
const content = tree.readContent('/projects/bar/src/app/bar/bar.module.ts');
329+
expect(content).toContain('RouterModule.forChild(routes)');
330+
expect(content).not.toContain('BarRoutingModule');
331+
});
291332
});
292333
});

0 commit comments

Comments
 (0)