Javascript 在重新调整浏览器大小之前,Ngonit中获取的数据不会显示

Javascript 在重新调整浏览器大小之前,Ngonit中获取的数据不会显示,javascript,angular,typescript,Javascript,Angular,Typescript,我在一个对话框的ngOnInit()中提取了一些数据。当对话框最初弹出时,没有显示任何数据。如果手动调整浏览器窗口的大小,它将显示。如果我在弹出窗口中导航到其他选项卡,然后返回到原始选项卡,数据也会出现。 我的恩戈尼尼特- ngOnInit(): void { this.route.params.pipe(takeUntil(this.destroy$)).subscribe( res => { this.itemId = res.itemId; this.getIte

我在一个对话框的
ngOnInit()
中提取了一些数据。当对话框最初弹出时,没有显示任何数据。如果手动调整浏览器窗口的大小,它将显示。如果我在弹出窗口中导航到其他选项卡,然后返回到原始选项卡,数据也会出现。 我的恩戈尼尼特-

ngOnInit(): void {
  this.route.params.pipe(takeUntil(this.destroy$)).subscribe( res => {
    this.itemId = res.itemId;
    this.getItemComponents(this.itemId);
    console.log(this.itemComponents);
}
加载页面时,console.log显示未定义。我在getItemComponents方法中放置了相同的console.log,如下所示:-

getItemComponents {
  this.itemService.getComponents.subscribe( res => {
    this.itemComponents = res;
    console.log(this.itemComponents);
}
上面的控制台日志记录了我想要的数据。
有办法吗?我已经尝试在HTML中使用ngIf,但没有任何帮助。

您的
控制台.log
方法在
ngOnInit
钩子启动之前,您的
getItemComponents
方法订阅完成,因此您需要将其合并到一个链中。尝试使用类似以下内容:

ngOnInit(): void {
    this.route.params
        .pipe(
            takeUntil(this.destroy$),
            switchMap(res => this.itemService.getComponents(res.itemId))
        )
        .subscribe( data => {
            this.itemComponents = data;
            console.log(this.itemComponents);
        }
}

您的
console.log
方法在
ngOnInit
hook启动的时间早于
getItemComponents
方法订阅完成的时间,因此您需要将其合并到一个链中。尝试使用类似以下内容:

ngOnInit(): void {
    this.route.params
        .pipe(
            takeUntil(this.destroy$),
            switchMap(res => this.itemService.getComponents(res.itemId))
        )
        .subscribe( data => {
            this.itemComponents = data;
            console.log(this.itemComponents);
        }
}