带有loadChildren的角默认路由redirectTo

时间:2018-10-22 09:42:05

标签: angular angular2-routing

我有以下应用程序:

const routes: Routes = [
  { path: '', redirectTo: 'demo', pathMatch: 'full' },
  { path: 'demo',  loadChildren: () => ChildModule },
];

imports: [
    RouterModule.forRoot(routes),
],

它正在loadChildren

中使用此模块
const routes: Routes = [
  {
    path: '',  component: DemoComponent,
    children: [
      { path: 'some', component: SomeComponent },
      { path: 'other', component: OtherComponent },
    ],
  },
];

imports: [
    RouterModule.forChild(routes),
],

如果我启动应用程序,则定义了两条路线:

http://localhost:4200/demo/some

http://localhost:4200/demo/other

也正在工作:

http://localhost:4200/some

http://localhost:4200/other

当我导航到http://localhost:4200/时,它没有重定向到http://localhost:4200/demo

那为什么/some/other路由有效而重定向无效?

1 个答案:

答案 0 :(得分:-1)

const routes: Routes = [
  { path: '', redirectTo: 'demo', pathMatch: 'full' },
  { path: 'someother',  loadChildren: () => ChildModule },
];

imports: [
    RouterModule.forRoot(routes),
],

=======================================================

const routes: Routes = [
  {
    path: "", redirectTo: "some", pathMatch: "full"
    },
    {
    path: "some",
    component: SomeComponent
    },
    {
    path: "other",
    component: OtherComponent
    }    

];

imports: [
    RouterModule.forChild(routes),
],