Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate component: Add an option to fine-tune the inclusion of lifecycle hooks #18771

Open
moniuch opened this issue Sep 12, 2020 · 0 comments
Open

Comments

@moniuch
Copy link

@moniuch moniuch commented Sep 12, 2020

🚀 Feature request

Command (mark with an x)

  • new
  • build
  • serve
  • test
  • e2e
  • generate
  • add
  • update
  • lint
  • xi18n
  • run
  • config
  • help
  • version
  • doc

Description

A clear and concise description of the problem or missing capability...

When sticking to the smart/dumb(presentational) components paradigm, user has no option to precisely control the implementation of lifecyclehooks in the generated TS code. Given that the default is implements OnInit, this leads to unnecessary post-editing (removing OnInit, or adding other hooks by hand). As you gain experience in angular, you are able to tell beforehand which hooks you're gonna need before you issue ng g c, but the CLI doesn't give you a way to fine-tune the generated code at the command level.

Describe the solution you'd like

If you have a solution in mind, please describe it.

Introduce --hooks flag, taking a piped string of hooks to implement, or a specific constant to include none. Examples:

ng g c mycomp --hooks none = ng g c mycomp -h
ng g c mycomp --hooks OnInit|OnDestroy|AfterViewInit

Pros:

  • provides a "compositional" granular opt-in model (as opposed to --skip* model which presumes existing defaults)
  • scalable (same syntax could also apply to directives, services, eg. ng g s myserv --hooks OnDestroy)

Cons:

  • not a fast-track solution

Having a flag to control the implements part would allow user to create custom scripts to follow the smart/dumb paradigm in their workflow, such as:

ng g c --hooks OnInit|OnDestroy|AfterViewInit -s false
   // usage: npm run csmart -- mycomp (= create mycomp, specified hooks, styles in ext file)

ng g c --hooks -c OnPush -s true
   // usage: npm run cdumb -- mycomp  (= create mycomp, no hooks, CD OnPush, inline style)

Describe alternatives you've considered

Have you considered any alternative solutions or workarounds?

I would gladly hear of one if exists.

Of course, the suggested flag --hooks (alias -h) is just an example - you might choose a different wording, such as --implements, however implements could suggest that user's custom or 3rd party interfaces/classes could be implemented too, while the option we discuss pertains to Angular lifecycle hooks only

A fast-track solution would be to provide --skipOnInit flag, but this is a non-scalable solution which I don't want to promote here.

@moniuch moniuch closed this Sep 12, 2020
@moniuch moniuch reopened this Sep 12, 2020
@ngbot ngbot bot added this to the Backlog milestone Sep 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.