Skip to main content
Clarify that this solution does not work for services.
Source Link
AJ Richardson
  • 6.8k
  • 1
  • 52
  • 62

Note: this answer applies only to Angular components and directives, NOT services.

I had this same issue when ngOnInit (and other lifecycle hooks) were not firing for my components, and most searches led me here.

The issue is that I was using the arrow function syntax (=>) like this:

class MyComponent implements OnInit { // Bad: do not use arrow function public ngOnInit = () => { console.log("ngOnInit"); } } 

Apparently that does not work in Angular 6. Using non-arrow function syntax fixes the issue:

class MyComponent implements OnInit { public ngOnInit() { console.log("ngOnInit"); } } 

I had this same issue when ngOnInit (and other lifecycle hooks) were not firing for my components, and most searches led me here.

The issue is that I was using the arrow function syntax (=>) like this:

class MyComponent implements OnInit { public ngOnInit = () => { console.log("ngOnInit"); } } 

Apparently that does not work in Angular 6. Using non-arrow function syntax fixes the issue:

class MyComponent implements OnInit { public ngOnInit() { console.log("ngOnInit"); } } 

Note: this answer applies only to Angular components and directives, NOT services.

I had this same issue when ngOnInit (and other lifecycle hooks) were not firing for my components, and most searches led me here.

The issue is that I was using the arrow function syntax (=>) like this:

class MyComponent implements OnInit { // Bad: do not use arrow function public ngOnInit = () => { console.log("ngOnInit"); } } 

Apparently that does not work in Angular 6. Using non-arrow function syntax fixes the issue:

class MyComponent implements OnInit { public ngOnInit() { console.log("ngOnInit"); } } 
Source Link
AJ Richardson
  • 6.8k
  • 1
  • 52
  • 62

I had this same issue when ngOnInit (and other lifecycle hooks) were not firing for my components, and most searches led me here.

The issue is that I was using the arrow function syntax (=>) like this:

class MyComponent implements OnInit { public ngOnInit = () => { console.log("ngOnInit"); } } 

Apparently that does not work in Angular 6. Using non-arrow function syntax fixes the issue:

class MyComponent implements OnInit { public ngOnInit() { console.log("ngOnInit"); } }