多个解析守卫,可以从任何路径访问每个

时间:2018-11-16 14:30:06

标签: angular router guard resolve

我需要从1个解析监护人访问另一个父母(从父母到孩子),其中“另一个”(孩子)具有异步数据,因此我如何从子路由到父路由获取此异步数据

这是一些代码

路径

const routes: Route[] = [
  { path: '', redirectTo: 'main', pathMatch: 'full' },
  { path: 'main', component: MainComponent },
  { path: 'project', 
    component: ProjectContainerComponent,  
    children: [
      { 
        path: ':id', 
        component: ProjectComponent, 
        data: {
          projectTitle: 'Все задачи',
          groupBy: 'Без группировки'
        },  
        resolve: {
          projectName: ProjectNameResolverService
        },
        children: [
          { path: '', redirectTo: 'list', pathMatch: 'full'},
          { path: 'list', component: ProjectIssueListContainerComponent, children: [
            { path: '', component: ProjectIssueListComponent },
            { path: ':issueId', component: ProjectIssueContainerComponent,
              resolve: {
                task: IssueResolverService
              }
            }
          ] },
          { path: 'create', component: CreateIssueComponent },

        ]
      }
    ]
  },
  { path: 'access_denied', component: AccessDeniedComponent }
]

孩子

export class IssueResolverService implements Resolve < any > {

  constructor(
    private projectsService: ProjectsService,
    private router: Router
  ) {}

  resolve(route: ActivatedRouteSnapshot) {
    const id = +route.paramMap.get('issueId');
    return this.projectsService.getProjectTask(id)
      .pipe(
        map((task: any) => {
          console.log(task);
          return task
        })
      );
  }

}

父母

export class ProjectNameResolverService {

  constructor() { }
  resolve(route: ActivatedRouteSnapshot) {
    console.log(route.firstChild.firstChild.data) // {} - but need to get it
  }
}

0 个答案:

没有答案