Skip to content

Commit df9fc8f

Browse files
Pawel Kozlowskidgp1130
Pawel Kozlowski
authored andcommitted
refactor(@schematics/angular): include CommonModule for standalone components
This commit adds the CommonModule imports to the standalone components generated by the Angular's ng add component schematic.
1 parent 269fb87 commit df9fc8f

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

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

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

34
@Component({<% if(!skipSelector) {%>
45
selector: '<%= selector %>',<%}%><% if(standalone) {%>
5-
standalone: true,<%}%><% if(inlineTemplate) { %>
6+
standalone: true,
7+
imports: [CommonModule], <%}%><% if(inlineTemplate) { %>
68
template: `
79
<p>
810
<%= dasherize(name) %> works!

packages/schematics/angular/component/index_spec.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,11 @@ describe('Component Schematic', () => {
426426
const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
427427
const moduleContent = tree.readContent('/projects/bar/src/app/app.module.ts');
428428
const componentContent = tree.readContent('/projects/bar/src/app/foo/foo.component.ts');
429-
expect(componentContent).toContain('standalone: true');
429+
expect(componentContent).toContain('@angular/common');
430430
expect(componentContent).toContain('class FooComponent');
431431
expect(moduleContent).not.toContain('FooComponent');
432+
expect(componentContent).toContain('standalone: true');
433+
expect(componentContent).toContain('imports: [CommonModule]');
432434
});
433435

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

0 commit comments

Comments
 (0)