Angular 5

时间:2018-04-14 10:23:39

标签: angular angular5 angular-routing angular-router

我有以下模块结构:

1- RootModule

路由如下:

const routes: Routes = [
  { path: '', redirectTo: 'app', pathMatch: 'full' }
];

2- AppModule

路由如下:

const routes: Routes = [
   { 
        path: 'app', 
        component: AppComponent,
        children: [
            {
                path: '',
                redirectTo: 'index',
                pathMatch: 'full'
           }
       ]
   }

];

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

const routes: Routes = [
    {
        path: '',
        component: MainComponent,
        children: [
            {
               path: 'index',
               loadChildren: '.\/..\/modules\/index\/index.module#IndexModule'
            },
            {
                path: '',
                redirectTo: 'index',
                pathMatch: 'full'
            }
       ]
  }

];

现在,RootComponent是起点:

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

  ngOnInit() {
 }
}

AppComponent定义为:

<router-outlet></router-outlet>
<app-quick-sidebar></app-quick-sidebar>

最后,MainComponent定义为:

<app-header-nav></app-header-nav>
<router-outlet></router-outlet>
<app-footer></app-footer>

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

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

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:3)

使用当前路由配置,MainComponent路径的子数组中未配置AppComponent。那么它为什么会出现在它的模板中?

现在您的路由配置将如下所示:

  • 导航至/app会转到AppComponent
  • 导航至/index会转到IndexComponent

实现RooComponent - &gt;所需的行为AppComponent - &gt; MainComponent - &gt; 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'
    }]
  }]
}];