Add flag to ng generate component that skips adding ngOnInit() interface.
See original GitHub issueπ Feature request
Command (mark with an x
)
- new
- build
- serve
- test
- e2e
- generate
- add
- update
- lint
- extract-i18n
- run
- config
- help
- version
- doc
Description
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
.
Issue Analytics
- State:
- Created a year ago
- Reactions:43
- Comments:8 (1 by maintainers)
Top Results From Across the Web
Add Feature Flags to Your Angular App in 10 Minutes - Split
We'll show you how to build a simple CRUD app using Angular components, services, models, and routing, and add feature flags branching byΒ ......
Read more >ng generate - Angular
This command has the following sub-commands: app-shell Β· application Β· class Β· component Β· directive Β· enum Β· guard Β· interceptor Β· interface...
Read more >How To Extend Classes with Angular Component Inheritance
ng new AngularComponentInheritance --style=css --routing --skip-tests. Copy. Note: We are passing some flag to the ng new command to add routingΒ ...
Read more >3 - Generating components and Nx lib - Angular and NgRx
--parentModule Update the router configuration of the parent module using loadChildren or children, depending on what `lazy` is set to. --tags Add tags...
Read more >Angular 11 CLI, "ng generate" option: --skip-tests
To make the change for all future generated code, modify your angular.json file by adding the skipTests:true to the schematics section.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
We started generating
ngOnInit
to encourage folks to not put complex logic in theconstructor
and 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.
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