简体   繁体   English

Angular viewchild在ngAfterViewInit中未定义

[英]Angular viewchild undefined in ngAfterViewInit

I have 2 components, ComponentA & ComponentB, comp b is a child to comp a. 我有2个组件,ComponentA和ComponentB,comp b是一个孩子来comp。 ComponentB basically takes input from ComponentA & generates view. ComponentB基本上从ComponentA获取输入并生成视图。 This input is obtained from a rest call. 此输入是从休息呼叫中获得的。 So I have wrapped my component b selector in an *ngIf. 所以我将组件b选择器包装在* ngIf中。 In this selector I also have a viewchild attached to it. 在这个选择器中,我还有一个viewchild附加到它。

So basically, what I am assuming is that when ComponentB is completely rendered, I want to execute some code using my viewchild reference. 所以基本上,我假设的是当ComponentB完全呈现时,我想使用我的viewchild引用执行一些代码。 This code is written in ngAfterViewInit() inside ComponentA. 此代码在ComponentA中的ngAfterViewInit()中编写。 But when I try to access this.viewChildReference, it says undefined. 但是当我尝试访问this.viewChildReference时,它表示未定义。

ComponentA.html ComponentA.html

<div *ngIf="dataAvailable">
    <componentb #viewChildReference [data]="dataFromComponentA"></componentb>
</div>

ComponentA.ts ComponentA.ts

// skipping boiler code for brevity

class {
    dataAvailable = false;
    @ViewChild('viewChildReference') viewChildReference: ChildComponentModule;

    ngOnInit() {
        // fetch data from rest
        dataAvailable = true;
    }

    ngAfterViewInit() {
        this.viewChildReference.somemethod();
    }
}

ComponentB.html ComponentB.html

// Use data from parent component and generate view

How can I solve this? 我怎么解决这个问题? What is wrong with my current approach? 我目前的做法有什么问题? Please help as I am stuck up at this point.. 请帮忙,因为我在这一点上被困住了..

UPDATE: 更新:

This question is not duplicate, as I have tried other solutions as suggested in comments in this question, like emitting events & other answers. 这个问题并不重复,因为我已经尝试过这个问题的评论中提出的其他解决方案,比如发出事件和其他答案。 Still I am facing issues, kindly unmark this as duplicate. 我仍然面临着问题,请将此标记为重复。

You can try 你可以试试

@ViewChild('viewChildReference') 
set viewChildReference(v) {
   // v is the viewChild, handle your logic here
   // if v is defined.... 
}

demo https://stackblitz.com/edit/angular-egh5dg , btw, this approach will always sync up with ngIf, if you use other approaches, viewChildReference might have stale reference, say, if you subscribe to api calls, but angular needs time to update viewChildReference and it could happen this view update is behind subscription. 演示https://stackblitz.com/edit/angular-egh5dg ,顺便说一句,这种方法将始终与ngIf同步,如果你使用其他方法,viewChildReference可能有陈旧的参考,比如,如果你订阅api调用,但角度需要更新viewChildReference的时间可能会发生此视图更新落后于订阅。

You said you're fetching the data from a rest api. 你说你正在从休息api中获取数据。 That's asynchronous. 这是异步的。 The call to ngAfterViewInit is synchronous, which means by the time it's called, your data is still not available, thus the component won't be rendered because of the ngIf . ngAfterViewInit的调用是同步的,这意味着在调用它时,您的数据仍然不可用,因此由于ngIf而不会呈现该ngIf You need to call this.viewChildReference.somemethod(); 你需要调用this.viewChildReference.somemethod(); once your http call completes. 一旦你的http呼叫完成。 Something like this: 像这样的东西:

this.myService.someAPiCall().subscribe(data => {
    this.data = data;
    this.viewChildReference.somemethod();
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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