Skip to content

Commit

Permalink
feat(@schematics/angular): remove ngOnInit from component template
Browse files Browse the repository at this point in the history
This commit removes the `constructor` and `ngOnInit` as most of the users prefer to add this manually if needed.

Closes #23678
  • Loading branch information
alan-agius4 authored and clydin committed Oct 4, 2022
1 parent 4b62346 commit 301b566
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { <% if(changeDetection !== 'Default') { %>ChangeDetectionStrategy, <% }%>Component, OnInit<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%> } from '@angular/core';<% if(standalone) {%>
import { <% if(changeDetection !== 'Default') { %>ChangeDetectionStrategy, <% }%>Component<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%> } from '@angular/core';<% if(standalone) {%>
import { CommonModule } from '@angular/common';<% } %>

@Component({<% if(!skipSelector) {%>
Expand All @@ -22,11 +22,6 @@ import { CommonModule } from '@angular/common';<% } %>
encapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } if (changeDetection !== 'Default') { %>,
changeDetection: ChangeDetectionStrategy.<%= changeDetection %><% } %>
})
export class <%= classify(name) %><%= classify(type) %> implements OnInit {

constructor() { }

ngOnInit(): void {
}
export class <%= classify(name) %><%= classify(type) %> {

}
4 changes: 2 additions & 2 deletions packages/schematics/angular/component/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ describe('Component Schematic', () => {
const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
const content = tree.readContent('/projects/bar/src/app/foo/foo.route.ts');
const testContent = tree.readContent('/projects/bar/src/app/foo/foo.route.spec.ts');
expect(content).toContain('export class FooRoute implements OnInit');
expect(content).toContain('export class FooRoute');
expect(testContent).toContain("describe('FooRoute'");
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.route.css');
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.route.html');
Expand All @@ -308,7 +308,7 @@ describe('Component Schematic', () => {
const tree = await schematicRunner.runSchematicAsync('component', options, appTree).toPromise();
const content = tree.readContent('/projects/bar/src/app/foo/foo.ts');
const testContent = tree.readContent('/projects/bar/src/app/foo/foo.spec.ts');
expect(content).toContain('export class Foo implements OnInit');
expect(content).toContain('export class Foo');
expect(testContent).toContain("describe('Foo'");
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.css');
expect(tree.files).toContain('/projects/bar/src/app/foo/foo.html');
Expand Down

1 comment on commit 301b566

@minercool
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just why ?

Please sign in to comment.