0

我有这些路由的路由模块文件:

const routes: Routes = [    
{
    path: '',
    component: MainComponent,
    canActivate: [LoadDataGuard, ConfigureJsGuard],
    canActivateChild: [EnterChildGuard],
    canDeactivate: [CleanupJsGuard],
    data: {
        modelService: ModelService
    },
    children: [
    {
        component: ChildComponent,
        .......

LoadDataGuard我编写了一个canActivate调用函数的enterTab函数(enterTab 管理子组件的数据。

这是我的代码:

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
    return this.modelService.loadData()
        .pipe(
            tap(data => {
                this.enterTab(modelService);
            }),
            map(data => true),
            catchError(err => this.handleError(err))
        }

这是enterTab功能:

private async enterTab(modelService: TabRepository) {
    if (modelService.enterTab) {
        return await modelService.enterTab();
    }
    return;
}

在模型服务中:

enterTab() {
    return this.modelApiService.getModelMetaData()
        .pipe(tap((result: any): Observable<any> => {
            this.modelMetaData = result.data; -> this data is received after canActivateChild starts.!!!
        }
    )).toPromise();
}

canActivateChild我正在使用modelService.modelMetaData但它是未定义的,因为子组件canActivate在父组件完成之前加载。

我该如何解决这个问题?

4

0 回答 0