You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add flag to ng generate component that skips adding ngOnInit() interface. #23678
Generating components with ng g c component-name always generates a component implementing ngOnInit() interface.
Many components do not benefit from implementing such interface. A good example might be a dumb component, that uses mostly @Input() and @Output().
Right now there is no way to override this behaviour so if you want to generate a dumb component, you need to:
remove ngOnInit() method,
remove implements OnInit clause from the component class,
remove the OnInit import from '@angular/core';
This has to be repeated for every component generated, so it gets old really fast 🤷
Describe the solution you'd like
Ideally, there should be an option to generate a component that does not implement the ngOnInit() hook.
Maybe something like this could work: ng g c component-name --skip-on-init or ng g c component-name --skip-lifecycle
It would match the naming of flags such as --skip-tests or --skip-selector.
Reacted by James Jarvis, Sergi Ávila, Ashley Hunter, Michał Stano, Zebzeev Artemiy, Dmitry A. Efimenko, damian, jase, Wilco Boshoff, Kristopher Betz and 24 more
I usually remove the ngOnit as well but I don't like the flag approach on the generator. I would prefer to set it up in the project config file and then add a flag when creating the project like ng new project --skip-lifecycle
While I do agree that the hook encourages devs to avoid stuffing the constructor, having it forced/hardcoded within the template takes away from the flexibility of having schematics. I think the same applies to the empty constructor as well.
It would be ideal to update the schema with properties that allow us to make them optional.
P.S:
Working on enterprise projects, this also interferes with shared linters that force us to avoid having empty hooks or contructors in the code before being allowed to push our changes. Its annoying to have to take that extra step to clear the code when we are trying to quickly test or prototype something.
@twerske, @mgechev
I think it makes sense to clean up the schematics so that it doesn't add the OnInit.
While on this, also remove the constructor. given the inject() function it makes less sense now to have it there by default.
one additional thought. Would it be possible to have the language server add/update the OnInit/OnDestroy/... implements fag when a user added one of the life-cycle hooks to the body of the class? Similar to automatic importing works?
Activity
alan-agius4 commentedon Aug 3, 2022
Thoughts @mgechev about removing
ngOnInitfrom the generated component code?Note: It's unlikely that we'd expose a flag to add/remove the
ngOnInit.muhammadawaisshaikh commentedon Aug 3, 2022
I want to take over this 🙌
ttkaae commentedon Aug 4, 2022
I usually remove the ngOnit as well but I don't like the flag approach on the generator. I would prefer to set it up in the project config file and then add a flag when creating the project like ng new project --skip-lifecycle
realtomaszkula commentedon Aug 4, 2022
I don't have a strong preference for having the --skip flag. As long as there is a way to generate the component without the ngOnInit() 👍
mgechev commentedon Aug 5, 2022
We started generating
ngOnInitto encourage folks to not put complex logic in theconstructorand make the component more testable.I, personally, keep the hook less than 1% of the time.
To me it makes sense to remove it from the component generator, but would like to collect more community feedback before we move forward.
ashish-koshy commentedon Sep 27, 2022
Thank you! @realtomaszkula for bringing attention to this.
@alan-agius4 @mgechev I was ready with a PR to have this added in.
While I do agree that the hook encourages devs to avoid stuffing the constructor, having it forced/hardcoded within the template takes away from the flexibility of having schematics. I think the same applies to the empty constructor as well.
It would be ideal to update the schema with properties that allow us to make them optional.
P.S:
Working on enterprise projects, this also interferes with shared linters that force us to avoid having empty hooks or contructors in the code before being allowed to push our changes. Its annoying to have to take that extra step to clear the code when we are trying to quickly test or prototype something.
SanderElias commentedon Sep 28, 2022
@twerske, @mgechev
I think it makes sense to clean up the schematics so that it doesn't add the
OnInit.While on this, also remove the constructor. given the
inject()function it makes less sense now to have it there by default.one additional thought. Would it be possible to have the language server add/update the
OnInit/OnDestroy/...implements fag when a user added one of the life-cycle hooks to the body of the class? Similar to automatic importing works?feat(@schematics/angular): remove `ngOnInit` from component template
feat(@schematics/angular): remove `ngOnInit` from component template
feat(@schematics/angular): remove `ngOnInit` from component template
angular-automatic-lock-bot commentedon Nov 4, 2022
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.