简体   繁体   中英

Angular2: ViewChild Referencing in constructor of class or in class body?

What is best practice and where is the difference between:

export class suchPage {
    @ViewChild(SuchDirective) suchDirective:SuchDirective;
}

and

export class suchPage {
    @ViewChild(SuchDirective);


    constructor(
        private suchDirective:SuchDirective
    ) {}
}

Official documentation references the first method. I'm guessing it's the best practice:

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

第二种方法不起作用,因为ViewChild注入的元素仅在视图初始化( ngAfterViewInit )之后可用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM