繁体   English   中英

Angular如何实现嵌套解析器

[英]Angular How to implement nested resolvers

我有一个使用解析器获取此类类别的组件:

ngOnInit() {
    this.category = this.route.snapshot.data['category'];
}

因此,这很不错,并且使我获得了类别,该类别实质上是用户列表。

从上述解析器获得用户名列表后,我已经在ngOnInit()中手动获得了用户帐户详细信息,但是当我将它们作为Input()传递给子组件时,它们尚未加载,

这将引发以下错误,这是有道理的。

CategoryItemComponent.html:4 ERROR TypeError: Cannot read property 'id' of undefined
at Object.eval [as updateRenderer] (CategoryItemComponent.html:4)
at Object.debugUpdateRenderer [as updateRenderer] (core.js:11940)
at checkAndUpdateView (core.js:11312)
at callViewAction (core.js:11548)
at execComponentViewsAction (core.js:11490)
at checkAndUpdateView (core.js:11313)
at callViewAction (core.js:11548)
at execEmbeddedViewsAction (core.js:11511)
at checkAndUpdateView (core.js:11308)
at callViewAction (core.js:11548)
at execComponentViewsAction (core.js:11490)
at checkAndUpdateView (core.js:11313)
at callViewAction (core.js:11548)
at execEmbeddedViewsAction (core.js:11511)
at checkAndUpdateView (core.js:11308)
at callViewAction (core.js:11548)

我的问题是:我可以使用第一个解析器的用户列表作为第二个解析器的输入。 如果是这样,这是如何完成的?

编辑1:

我目前将从ngOnInit()下载的用户提供给子组件,如下所示:

<app-account [account]="account"></app-account>

我知道我可以通过@alokstar来回答,但我想知道这是否是推荐的方法-还是有一种更优雅的嵌套解析器的方法。

因此,如果需要此输入来加载子组件,那么您可以考虑在条件加载之前将条件置于子组件上,例如:

<child-component *ngIf = 'dataAvailable'>
</child-component>

或者,即使输入未定义或不可用,也要加载子组件,则可以在子组件中添加防御条件,以避开“未定义”类型的错误。

暂无
暂无

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

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