question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:

  1. remove ngOnInit() method,
  2. remove implements OnInit clause from the component class,
  3. 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:closed
  • Created a year ago
  • Reactions:43
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
mgechevcommented, Aug 5, 2022

We started generating ngOnInit to encourage folks to not put complex logic in the constructor 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.

3reactions
y-nutcommented, 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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found