Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESLint complaining about empty constructor and ngOnInit-implementation

In my Angular application, the rule no-empty-function, triggered an error for my constructor. Well, it is indeed an empty body there but the constructor itself needs to be there, because I inject a service.

export class ClientListComponent implements OnInit {
  constructor(service: AuxService) { }
  ngOnInit() { }
  ...
}

Interestingly, it also complains about the implemented method for the interface. However, the error messages vary between those two, which perplexes me additionally.

Unexpected empty constructor.
Unexpected empty method 'ngOnInit'.

So, it clearly distinguishes between a plain method (be that still required one due to the implemented interface, which itself is wrong in my opinion to nag about) and a constructor. I'm sure that the creators have heard about dependency injection, so I can't understand what I'm missing.

like image 508
Konrad Viltersten Avatar asked Mar 14 '21 13:03

Konrad Viltersten


People also ask

How do I disable Eslint no empty function?

If you want to disable it simply add "no-empty": false, to your tslint. json (globally disable) or disable it inline using a /* tslint:disable:no-empty */ comment.

Can ngOnInit be empty?

I never leave an empty ngOnInit in any file in my projects. The tree shaking when it builds will remove it anyways, but for readability it cleans up the components. As if you leave it, you'll have an import, an implement and a code reference that are all unused.


1 Answers

You also could modify your ".eslintrc.json" file to exclude the rule from the linter.

enter image description here

like image 115
Jason Glez Avatar answered Oct 20 '22 21:10

Jason Glez