簡體   English   中英

Angular2:ViewChild是在類的構造函數中還是在類主體中引用?

[英]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
    ) {}
}

官方文檔引用第一種方法。 我猜這是最佳做法:

https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#parent-to-view-child

第二種方法不起作用,因為ViewChild注入的元素僅在視圖初始化( ngAfterViewInit )之后可用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM