Angular 2嵌套路由解析执行

时间:2016-09-07 08:25:45

标签: angular typescript router resolve

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

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将首先执行并同时发出请求。

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

Angular 2 RC6,Angular router 3.0.0-rc.2

1 个答案:

答案 0 :(得分:3)

我知道这个问题很老,但万一有人偶然发现它(像我一样)。

这是一个已知的错误,并且已经修复。只需将您的路由器版本更新为高于或等于2.1.0的版本,您应该好好去。这里的参考是相关的issue on githubassociated fix