首页 > 解决方案 > Angular 中子组件的问题

问题描述

这是我的项目:https ://stackblitz.com/edit/planificador?file=src/app/planificador/components/resumen/resumen.component.ts

我有一个 MainContentComponent (父级),其中有几个子组件

主要内容.html

现在我看到在 ContratosComponent 内部例如完美地得到 proyectoId 但在 ResumenComponent 我得到未定义

不明确的

ngOnInit

输入

请问有什么想法吗?

标签: angularparent-childangular-components

解决方案


我运行stackblitz,似乎两个组件都正确返回了proyectoId - 所以我认为它可能也取决于其他一些东西并且不容易复制?

您的问题可能是proyecto$observable 来自BehaviourSubject. 这意味着如果没有将值推送到该主题,它将发出 null。

<div *ngIf="proyecto$ | async as proyecto">一旦 observable 发出,您的 ngIf就会返回 true。因此,如果没有将 proyecto 推送到BehaviourSubject,它将返回 true 并且proyecto将为 null。这可能是您的问题的原因。

同样,这很难说,因为似乎有一些竞争条件导致问题并不总是发生。


推荐阅读