[英]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 https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#parent-to-view-child
第二种方法不起作用,因为ViewChild
注入的元素仅在视图初始化( ngAfterViewInit
)之后可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.