DEV Community

Discussion on: The Angular @ViewChild decorator

Collapse
 
zooly profile image
Hugo Torzuoli

Good to see dev of French company posting about Angular!
Thanks for this article, very interesting.

Concerning code style, is there a reason why you declare your ViewChild attribute on the same line of the decorator?
I think your two last examples are more readable than the first ones.

Collapse
 
korbraan profile image
Cédric Rémond

Haha, hello from France 😄

Actually at work I use Prettier to enforce a coherent code style.
A side effect is that I don't often think about coding style anymore.

When I use ViewChild without any parameter (which impossible with Angular 8), everything fit well on one line. With parameters, I also find the two last examples more readable.

Collapse
 
zooly profile image
Hugo Torzuoli

Ok yes I understand now!
That said, I've checked in my work project, 90% of ViewChild are one-lined haha. But we're using Angular 6 at this time.
That means that if I want to upgrade from Angular 6 to 8, I will have to "rewrite" all my ViewChild declarations ?

Thread Thread
 
korbraan profile image
Cédric Rémond • Edited

Basically yes.
However the CLI will help you as much as it can. The ng upgrade command will, for each ViewChild in your code base, try to detect if it is used statically or dynamically and then add the static parameter itself. If it cannot be determined by the static analysis of the CLI, a TODO comment will be added above each ViewChild to update.

Thread Thread
 
zooly profile image
Hugo Torzuoli

Indeed they handle this case in the upgrade process :) Thanks for explanation!

upgradeViewChild