Is it okay to mark a ViewChild as private, if it's not being referenced in the template?
I have been able to do this, then build and serve with the '--prod' flag, and encountered no errors. I'm currently using Angular 7.
@ViewChild(HelloComponent) private helloComponent;
Here's a stackblitz to what I'm talking about:
https://stackblitz.com/edit/angular-vxbpuk?file=src%2Fapp%2Fapp.component.ts
I think I have the stackblitz using aot, but you could verify the same thing locally.
EDIT: I apologize for not bringing this up before, but there's this blurb from the angular doc that give me pause:
https://angular.io/guide/aot-compiler#phase-2-code-generation
Decorated component class members must be public. You cannot make an @Input() property private or internal.
But as @GCSDC already pointed out below, the Angular team seems to be using private members in their examples:
https://angular.io/guide/component-interaction#parent-calls-an-viewchild
Conclusion. The @ViewChild decorator allows us to inject into a component class references to elements used inside its template, that's what we should use it for. Using @ViewChild we can easily inject components, directives or plain DOM elements.
While Angular inputs/outputs should be used when sharing data to and from child components, ViewChild should be used when trying to utilize properties and methods of the child component directly in the parent component.
Working with @ViewChildren is similar to @ViewChild, but the difference between the two is @ViewChildren provides a list of element references rather than returning a single reference. It is used to reference multiple elements. We can then iterate the list of the element referenced by the variable.
Access In Component Lifecycle Hooks Setting static to true or false does affect which lifecycle hook you can access the ViewChild, but it's more a side effect of what static is actually doing rather than the intention of the property.
It seems that yes, this is ok, as you may find it done also on the official angular fundamentals guides, at:
Components & Templates > Component Interaction > Parent calls an @ViewChild():
@ViewChild(CountdownTimerComponent) private timerComponent: CountdownTimerComponent;
The private
and public
syntax is an enhancement for the javascript language for "static analyze" in typescript transpiler, I would recommend you to try the playground how typescript transpiles to JS. So basically it's a way for anyone reading the code get a sense of what the scope is for the variables/functions.
Example of a simple variable inside a class would be that they both transpiles into the same JS private var1: number = 0
and public var1: number = 0
transpiles both to this.var1 = 0
However! There could be situations you would actually like to access @ViewChild from a parent component, but in that situation you have to put it as @ViewChild(HelloComponent) public helloComponent;
or you will get "compile/transpile" error. If you want to give a more clearer feeling on where the scope would be keep it as private
and instead use getters/setters like so:
export class MyComponent {
@ViewChild(HelloComponent) private _helloComponent;
get helloComponent(): any {
return _helloComponent;
}
set helloComponent(set: any) {
this._helloComponent = set;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With