Angular 5中的嵌套路由

Bil*_*ill 5 angular-routing angular angular-router angular5

我有以下模块结构:

1- RootModule

路由如下:

const routes: Routes = [
  { path: '', redirectTo: 'app', pathMatch: 'full' }
];
Run Code Online (Sandbox Code Playgroud)

2- AppModule

路由如下:

const routes: Routes = [
   { 
        path: 'app', 
        component: AppComponent,
        children: [
            {
                path: '',
                redirectTo: 'index',
                pathMatch: 'full'
           }
       ]
   }
Run Code Online (Sandbox Code Playgroud)

].

此外,AppModule导入MainModule只是一个路由模块,配置如下:

const routes: Routes = [
    {
        path: '',
        component: MainComponent,
        children: [
            {
               path: 'index',
               loadChildren: '.\/..\/modules\/index\/index.module#IndexModule'
            },
            {
                path: '',
                redirectTo: 'index',
                pathMatch: 'full'
            }
       ]
  }
Run Code Online (Sandbox Code Playgroud)

].

现在,RootComponent是起点:

@Component({
  selector: "app-root",
  template:  "<router-outlet></router-outlet>"
})
export class RootComponent implements OnInit {
  constructor() { }

  ngOnInit() {
 }
}
Run Code Online (Sandbox Code Playgroud)

AppComponent定义为:

<router-outlet></router-outlet>
<app-quick-sidebar></app-quick-sidebar>
Run Code Online (Sandbox Code Playgroud)

最后,MainComponent定义为:

<app-header-nav></app-header-nav>
<router-outlet></router-outlet>
<app-footer></app-footer>
Run Code Online (Sandbox Code Playgroud)

重点是将应用程序路由到/ index组件,以便通过RooComponent- > AppComponent- > MainComponent- >IndexComponent

到目前为止,通过以上路线,AppComponent被绕过!

任何的想法?

谢谢

Tom*_*ula 4

根据您当前的路由配置,MainComponent未在 的AppComponent路径的子数组中进行配置。那么为什么它会出现在它的模板中呢?

现在你的路由配置将像这样工作:

  • 导航至/app将使您到达AppComponent
  • 导航至/index将使您到达IndexComponent

RooComponent要实现--> AppComponent--> MainComponent-->所需的行为IndexComponent,您的路由配置应如下所示:

const routes: Routes = [{ 
  path: '', 
  component: AppComponent,
  children: [{
    path: '',
    component: MainComponent,
    children: [{
      path: '', redirectTo: 'index', pathMatch: 'full'
    }, {
      path: 'index',
      loadChildren: '.\/..\/modules\/index\/index.module#IndexModule'
    }]
  }]
}];
Run Code Online (Sandbox Code Playgroud)