The article discusses the transition from using @ViewChild and @ViewChildren decorators to the new signal queries viewChild and viewChildren in Angular applications.
Abstract
Angular developers are encouraged to shift from the traditional @ViewChild and @ViewChildren decorators to the new viewChild and viewChildren signal queries. This change is part of Angular's move towards a more modern, signals-based approach, which offers a different way of thinking that is powerful and flexible. The article provides a tutorial on how to convert existing code that uses the old decorators to the new signal queries, demonstrating the process with a practical example of focusing an input element and counting child components. The tutorial illustrates the ease of use and benefits of the new signal queries, which simplify the code and improve the reactivity of Angular applications.
Opinions
The author believes that the new signal queries are an improvement over the old decorators, describing them as "easy to use, powerful, and flexible."
The author suggests that the switch to signals represents a change in thinking for Angular developers, implying that it may require some adjustment but is ultimately beneficial.
The author emphasizes the advantages of the new approach, such as making working with components and templates "a breeze."
The article implies that developers should embrace the new signal queries as they provide a better way to handle view queries in Angular applications.
The author encourages readers to "ditch those old decorators for good" and to refer to their other Angular tutorials for more tips and tricks, indicating a strong endorsement of the new features.
Stop Using @ViewChild/Children Decorators! Use Signals Instead
If you’ve been working with Angular for very long, you’re probably pretty familiar with the @ViewChild and @ViewChildren decorators. Well, if you haven’t heard yet, the framework is moving away from these decorators in favor of the new viewChild and viewChildren signal query functions. This means that it’s time for us to make the switch! In this tutorial we’ll take an existing example of both decorators and convert them each to the new respective functions.
Let’s switch this concept and update it to use signals.
First, we can remove the decorator, then we’ll set this property to a signal using the new viewChild function. We’ll need to be sure to import this function from the @angular/core module.
When we save now it should all be working as expected.
The count should be correct right out of the gate. Then as we filter, we should still see the count update as the list changes.
So, it should all work just like it did before, but now instead of using the old decorator and QueryList, it’s done using a more modern signals-based approach.
In Conclusion
The new viewChild and viewChildren functions are a change for Angular developers for sure.
When switching to signals, it’s a different way of thinking, but these functions are easy to use, powerful, and flexible, and they make working with components and templates a breeze.
Check out the demo code and examples of these techniques in the in the Stackblitz example below. If you have any questions or thoughts, don’t hesitate to leave a comment.