![](/img/trans.png)
[英]Angular2 Components :: ViewChild / ContentChild by class
[英]Angular2: ViewChild Referencing in constructor of class or in class body?
最佳實踐是什么,以及兩者之間的區別是什么?
export class suchPage {
@ViewChild(SuchDirective) suchDirective:SuchDirective;
}
和
export class suchPage {
@ViewChild(SuchDirective);
constructor(
private suchDirective:SuchDirective
) {}
}
第二種方法不起作用,因為ViewChild
注入的元素僅在視圖初始化( ngAfterViewInit
)之后可用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.