Angular 2嵌套路由解析执行 [英] Angular 2 nested routes resolve execution

查看:110
本文介绍了Angular 2嵌套路由解析执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我具有以下路线组织:

For example if I have following route organization:

const appRoutes: Routes = [
    {
        path: "",
        component: AppComponent,
        resolve: {
            app: AppResolver
        },
        children: [
            {
                path: "",
                component: NestedComponent,
                resolve: {
                    subscribers: NestedResolver
                }
            }
        ]
    }
];

和以下解析器:

export class AppResolver implements Resolve<any> {
    constructor(private appService: AppService) {}
    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> {
        return this.appService.getAppData();
    }
}

export class NestedResolver implements Resolve<any> {
    constructor(private nestedService: NestedService) {}
    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> {
        console.log(route.parent.data); //when this is executed route.parent.data is empty :(
        return this.nestedService.getNestedData();
    }
}

应用程序引导后,NestedResolver和AppResolver将首先执行并并行发出其请求.

After app bootstraping NestedResolver and AppResolver will execute first and make their requests in parallel.

我们可以更改代码并实现NestedResolver等待AppResolver解析并有权访问AppResolver解析的数据吗?

Can we change code and implement that NestedResolver waits for AppResolver to resolve and has access to AppResolver resolved data?

Angular 2 RC6,Angular路由器3.0.0-rc.2

Angular 2 RC6, Angular router 3.0.0-rc.2

推荐答案

我知道这个问题很老,但以防万一有人偶然发现(例如我).

I know this question is pretty old but just in case someone stumble upon it (like me).

这是一个已知的错误,此问题已得到修复.只需将您的路由器版本更新为高于或等于2.1.0,您就可以使用了.供参考的是相关的 github上的问题

This was a known bug and it has since been fixed. Just update your router version to something higher or equal to 2.1.0 and you should be good to go. For reference here is the relevant issue on github and the associated fix

这篇关于Angular 2嵌套路由解析执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆