如何将解析器应用于多个路由

时间:2018-07-11 18:11:26

标签: angular angular-router

我有一个包含多个路由的路由配置,每个路由都有相同的解析器,除了重定向。我想要一种更整洁的方法,将解析器应用于除重定向之外的所有这些路由。

我之前在StackOverflow上遇到过类似问题的解决方案,但是我一生都找不到。这个问题可能并不完全相同,因为重定向路由不会应用解析器。

export const routes: Routes = [
  {
    path: '',
    redirectTo: 'contest',
    pathMatch: 'full',
  },
  {
    path: 'contest',
    component: ContestPageComponent,
    pathMatch: 'full',
    resolve: DelayResolver,
  },
  {
    path: 'rules',
    component: RulesPageComponent,
    pathMatch: 'full',
    resolve: DelayResolver,
  },
  {
    path: 'experience',
    component: ExperiencePageComponent,
    pathMatch: 'full',
    resolve: DelayResolver,
  },
  {
    path: 'enter',
    component: EnterPageComponent,
    pathMatch: 'full',
    resolve: DelayResolver,
  },
  {
    path: 'about',
    component: AboutPageComponent,
    pathMatch: 'full',
    resolve: DelayResolver,
  },

];

2 个答案:

答案 0 :(得分:0)

    const routes: Routes = [
  {
    path: '',
    component: ContestWrapperComponent,
    children: [
      {
        path: 'contest',
        component: AboutPageComponent
      },
      {
        path: 'rules',
        component: RulesPageComponent,
      },
     {
        path: 'experience',
        component: ExperiencePageComponent,
      },
     {
        path: 'enter',
        component: EnterPageComponent,
      },
     {
        path: 'about',
        component: ExperiencePageComponent,
      }

    ],
   resolve:DelayResolver
  }
];

答案 1 :(得分:0)

创建没有组件的父路由。

export const routes: Routes = [
    {
        path: '',
        redirectTo: 'contest',
        pathMatch: 'full',
    },
    {
        path: '',
        resolve: {
            Data: DataResolver
        },
        children: [
            {
                path: 'contest',
                component: ContestPageComponent,
                pathMatch: 'full',
                resolve: DelayResolver,
            },
            {
                path: 'rules',
                component: RulesPageComponent,
                pathMatch: 'full',
                resolve: DelayResolver,
            },
            {
                path: 'experience',
                component: ExperiencePageComponent,
                pathMatch: 'full',
                resolve: DelayResolver,
            },
            {
                path: 'enter',
                component: EnterPageComponent,
                pathMatch: 'full',
                resolve: DelayResolver,
            },
            {
                path: 'about',
                component: AboutPageComponent,
                pathMatch: 'full',
                resolve: DelayResolver,
            }
        ]
    }
]

我不确定重定向是在空父之前还是之后,但是它不起作用。尝试将重定向移到底部。