UI 路由器重定向到承诺后从 ResolveFn


我正在使用 UI 路由器的 Angular

2+(针对 Angular 5(版本,但如果有人有资源来解决使用 Angular 1 版本的相同问题,我会很高兴看到 - 但我知道 redirectTo 属性不是早期版本的 UI 路由器的原始功能。

我有一个父组件,它的模板具有"选项卡",每个选项卡都命名为 <ui-view> s。

父组件模板:

<ui-view name="child1" *ngIf="isCurrentView()"></ui-view>
<ui-view name="child2" *ngIf="isCurrentView()"></ui-view>
...

有了这个,您可以看到父模板中除了这些"选项卡"之外没有任何内容。因此,我希望名为"child1"的ui-view成为默认视图,并且我希望它始终处于父视图的活动状态,直到用户导航到其他路由以打开另一个 ui 视图。

假设 isCurrentView() 函数有效,并为任何应该可见的视图返回一个布尔值。一次只能有一个视图可见。我包含此信息只是为了清楚起见。

状态以这种方式设置。您注意到在父组件状态中有多个resolveFn。这些都调用 API 并且是异步的,在这些完成之前,我们无法导航到任何"选项卡"。此设置可确保情况确实如此。子项 1 和子项 2 组件都依赖于具有这些响应。

{
    name: "parentComponent",
    url: "/parent",
    component: ParentComponent,
    resolve: [
        {
            token: "apiDetail1",
            deps: [MyAPIService],
            resolveFn: (myAPIService) => { return myAPIService.getApiDetail1(); }
        },
        {
            token: "apiDetail2",
            deps: [MyAPIService],
            resolveFn: (myAPIService) => { return myAPIService.getApiDetail2(); }
        }
    ]
},
{
    name: "parentComponent.child1",
    url: "/child1",
    views: {
        child1: { component: Child1Component }
    }
},
{
    name: "parentComponent.child2",
    url: "/child2",
    views: {
        child2: { component: Child2Component } 
    }
}

我想在所有解析函数完成后立即重新路由到名为 parentComponent.child1 的状态 - 否则必要的数据将不可用。但我不知道该怎么做。

我知道 UI 路由器有一个"重定向到"属性,然后我可以在目标状态下传递该属性,如下所示:

redirectTo: "parentComponent.child1"

但是,重定向到在路由激活开始时完成。我知道我可以传入异步服务并让重定向等待此异步服务完成 - 但我不确定如何使用实际上在同一路由中等待解析函数的服务。

我已经调用了这些 API 一次,所以我不需要再次调用它们。我只想在重定向之前等待已经调用的两个。但是怎么做呢?

在移动了一些不同的东西之后,我找到了答案。这有效,但仍会生成我尚未诊断的控制台错误。如果有人能提供更多见解或更深入的答案,我将不胜感激。

我最终做的第一件事就是删除所有多个<ui-view>标签。我只在父组件模板中留下了一个未命名的标签。只有一个,我也不需要神秘的isCurrentView()功能。

父组件模板

<ui-view></ui-view>

然后,在我的状态中,我以这种方式定义了redirectTo函数:

{
    name: "parentComponent",
    url: "/parent",
    component: ParentComponent,
    resolve: [
        {
            token: "apiDetail1",
            deps: [MyAPIService],
            resolveFn: (myAPIService) => { return myAPIService.getApiDetail1(); }
        },
        {
            token: "apiDetail2",
            deps: [MyAPIService],
            resolveFn: (myAPIService) => { return myAPIService.getApiDetail2(); }
        }
    ],
    redirectTo: (transitionService: Transition) => { // This service is of type Transition, the same object you would grab parameters from
        const apiCall1Promise = transitionService.injector().getAsync("apiDetail1"); 
        const apiCall2Promise = transitionService.injector().getAsync("apiDetail2");
        // I getAsync the same tokens I defined in my resolve functions
        return Promise.all([apiCall1Promise, apiCall2Promise]).then(()=>{
            // I don't need any of the results of the promises, since I already grab the information I need in the resolve functions, so I don't worry about getting the response here.
            return "parentComponent.child1"; // Since I need to wait for both calls, I use Promise.all instead of calling the promises directly.
        }).catch(() => {
            return "parentComponent.child1"; // This is likely bad form, but I want it to always redirect to this state, since I do not have anything else to show the user on the current parentComponent state.
        });
    }
},
{
    name: "parentComponent.child1",
    url: "/child1",
    component: Child1Component // since I'm not using named ui-view tags, I moved info out of the views property into the component property
},
{
    name: "parentComponent.child2",
    url: "/child2",
    component: Child2Component 
}        

}

通过使用getAsync((将令牌绑定到transitionService.injector((,然后重定向将等待承诺返回。我等待我的两个解析(由他们的令牌引用(,一旦两者都完成,我返回我希望它导航到的路由。

但是,以这种方式完成时,我仍然会收到此错误:

Transition Rejection($id: 5 type: 2, message: The transition has been superseded by a different transition, detail: Transition#7( 'routeImNavigatingFrom'{} -> 'parentComponent.child1'{} ))

我不清楚这个错误是什么意思或为什么我会收到它,因为我已经遵循了重定向到文档。此处正在讨论错误消息。

最新更新