Skip to content

Commit 571722d

Browse files
cexbrayatalan-agius4
authored andcommitted
fix(@schematics/angular): remove CommonModule import from standalone components
`ng g c` no longer generates a component with the `CommonModule` import. This import is not useful if developers adopt the control flow syntax in their templates. Instead, developers are encouraged to import the individual directives/pipes if needed.
1 parent ba8541e commit 571722d

File tree

2 files changed

+2
-5
lines changed

2 files changed

+2
-5
lines changed

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { <% if(changeDetection !== 'Default') { %>ChangeDetectionStrategy, <% }%>Component<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%> } from '@angular/core';<% if(standalone) {%>
2-
import { CommonModule } from '@angular/common';<% } %>
1+
import { <% if(changeDetection !== 'Default') { %>ChangeDetectionStrategy, <% }%>Component<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%> } from '@angular/core';
32

43
@Component({<% if(!skipSelector) {%>
54
selector: '<%= selector %>',<%}%><% if(standalone) {%>
65
standalone: true,
7-
imports: [CommonModule],<%}%><% if(inlineTemplate) { %>
6+
imports: [],<%}%><% if(inlineTemplate) { %>
87
template: `
98
<p>
109
<%= dasherize(name) %> works!

packages/schematics/angular/component/index_spec.ts

-2
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,9 @@ describe('Component Schematic', () => {
340340
const tree = await schematicRunner.runSchematic('component', options, appTree);
341341
const moduleContent = tree.readContent('/projects/bar/src/app/app.module.ts');
342342
const componentContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
343-
expect(componentContent).toContain('@angular/common');
344343
expect(componentContent).toContain('class FooComponent');
345344
expect(moduleContent).not.toContain('FooComponent');
346345
expect(componentContent).toContain('standalone: true');
347-
expect(componentContent).toContain('imports: [CommonModule]');
348346
});
349347

350348
it('should declare standalone components in the `imports` of a test', async () => {

0 commit comments

Comments
 (0)