繁体   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