Angular如何实现嵌套的解析器

时间:2018-11-21 19:20:01

标签: angular angular-resolver

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

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回答,但是我想知道这是否是推荐的方法-还是有一种更优雅的嵌套解析器的方法。

1 个答案:

答案 0 :(得分:1)

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

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

或者,即使输入未定义或不可用,如果您想加载子组件,也可以在子组件中添加防御条件,以摆脱“未定义”的错误。