Skip to content

Commit 301b566

Browse files
alan-agius4clydin
authored andcommitted
feat(@schematics/angular): remove ngOnInit from component template
This commit removes the `constructor` and `ngOnInit` as most of the users prefer to add this manually if needed. Closes #23678
1 parent 4b62346 commit 301b566

File tree

2 files changed

+4
-9
lines changed

2 files changed

+4
-9
lines changed

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { <% if(changeDetection !== 'Default') { %>ChangeDetectionStrategy, <% }%>Component, OnInit<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%> } from '@angular/core';<% if(standalone) {%>
1+
import { <% if(changeDetection !== 'Default') { %>ChangeDetectionStrategy, <% }%>Component<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%> } from '@angular/core';<% if(standalone) {%>
22
import { CommonModule } from '@angular/common';<% } %>
33

44
@Component({<% if(!skipSelector) {%>
@@ -22,11 +22,6 @@ import { CommonModule } from '@angular/common';<% } %>
2222
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
2323
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>
2424
})
25-
export class <%= classify(name) %><%= classify(type) %> implements OnInit {
26-
27-
constructor() { }
28-
29-
ngOnInit(): void {
30-
}
25+
export class <%= classify(name) %><%= classify(type) %> {
3126

3227
}

packages/schematics/angular/component/index_spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ describe('Component Schematic', () => {
297297
const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
298298
const content = tree.readContent('/projects/bar/src/app/foo/foo.route.ts');
299299
const testContent = tree.readContent('/projects/bar/src/app/foo/foo.route.spec.ts');
300-
expect(content).toContain('export class FooRoute implements OnInit');
300+
expect(content).toContain('export class FooRoute');
301301
expect(testContent).toContain("describe('FooRoute'");
302302
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.route.css');
303303
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.route.html');
@@ -308,7 +308,7 @@ describe('Component Schematic', () => {
308308
const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
309309
const content = tree.readContent('/projects/bar/src/app/foo/foo.ts');
310310
const testContent = tree.readContent('/projects/bar/src/app/foo/foo.spec.ts');
311-
expect(content).toContain('export class Foo implements OnInit');
311+
expect(content).toContain('export class Foo');
312312
expect(testContent).toContain("describe('Foo'");
313313
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.css');
314314
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.html');

0 commit comments

Comments
 (0)