AngularのViewChildrenが動かない時に確認すべきこと (Angular2, 4, 5)
ViewChildren
は、 ViewChild
と違い、 AfterViewInit
の前に作成される。
そのため、 ngOnInit
で取得しようとしても動かない。
ハマりどころなので注意。
公式ドキュメントに書いてはあるんだけど、見逃す。
https://angular.io/api/core/ViewChildren
使い方
export class TestComponent implements OnInit, AfterViewInit {
@ViewChildren(KoComponent) koComponents: QueryList<KoComponent>;
ngOnInit(): void {
const test = this.koComponents; // -> null
}
ngAfterViewInit(): void {
const test = this.koComponents; // -> 値が入ってる
}
}
export class TestComponent implements OnInit, AfterViewInit {
@ViewChildren(KoComponent) koComponents: QueryList<KoComponent>;
ngOnInit(): void {
const test = this.koComponents; // -> null
}
ngAfterViewInit(): void {
const test = this.koComponents; // -> 値が入ってる
}
}
注意しましょう。
Author And Source
この問題について(AngularのViewChildrenが動かない時に確認すべきこと (Angular2, 4, 5)), 我々は、より多くの情報をここで見つけました https://qiita.com/seteen/items/c92e31d0db51070fb342著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .