Angular 2 - Submodule routing and nested

后端 未结 3 1750
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 08:46

I\'m looking for a solution with Angular 2 for the scenario explained below:

In this scenario, the top-nav contains links to load submodules and sub-nav has

相关标签:
3条回答
  • 2020-12-07 09:42

    you have to mention the outlet name in the routes mention your router outlet name in the routing like this "outlet:'sub"

    routes: Routes = [
      {path:'', redirectTo: 'login', pathMatch: 'full'},
      {
        path: 'login',
        component: LoginComponent,
    
      },
      { path: 'home',
        component: AppComponent,
          children: [
            {path: 'home/pdf',component: SideMenuPage,outlet:"sub" },
            {path:'home/addFileUp',component:SidePageAdd,outlet:"sub"},
          ]
       },
    ];
    
    0 讨论(0)
  • 2020-12-07 09:44

    Use:

    RouterModule.forChild()
    ...
    <router-outlet name="sub"></router-outlet>
    ...
    [routerLink]="[{ outlets: { sub: [subRouteName] } }]"
    

    Full example:

    HTML

    <div class="tabs tinyscroll">
      <button *ngFor="let tab of tabs"
      [routerLink]="[{ outlets: { sub: [tab.name] } }]"
      routerLinkActive="selected">
        <span>{{ tab.label }}</span>
      </button>
    </div>
    
    <section>
      <router-outlet name="sub"></router-outlet>
    </section>
    

    app.module.ts

    imports: [
    ...
        RouterModule.forChild([
          {
            path: 'registers',
            component: RegistersComponent,
            children: [
              {path: 'vehicles', component: VehiclesComponent, outlet: 'sub'},
              {path: 'drivers', component: DriversComponent, outlet: 'sub'},
              {path: 'bases', component: BasesComponent, outlet: 'sub'},
              {path: 'lines', component: LinesComponent, outlet: 'sub'},
              {path: 'users', component: UsersComponent, outlet: 'sub'},
              {path: 'config', component: ConfigComponent, outlet: 'sub'},
              {path: 'companies', component: CompaniesComponent, outlet: 'sub'}
            ],
            canActivate: [AuthGuard]
          }
        ]),
    ...
    
    0 讨论(0)
  • 2020-12-07 09:48

    The html page will look like this.

    Main Page

    <top-navbar></top-navbar>
    <router-outlet></router-outlet>
    

    Sub Module Page

    <sub-navbar></sub-navbar>
    <router-outlet name='sub'></router-outlet>
    

    on clicking navigation in top-nav bar the main route outlet will route respectively.

    while clicking on sub-navbar the router-outlet [sub] will route respectively.

    HTML is fine, the trick will came at writing app.routing

    app.routing.ts

    const appRoutes: Routes = [
      {
        path: 'login',
        component: LoginComponent
      },
      { path: 'home',
        component: homeComponent,
        children: [
          {
            path: 'module1',
            component: module1Component,
            children: [
              {
                path: 'submodule11',
                component: submodule11Component,
              },
              {
                path: '',
                redirectTo: 'submodule11',
                pathMatch: 'full'
              }
            ]
          },
          {
            path: 'module2',
            component: module2omponent,
            children: [
              {
                path: 'submodule21',
                component: submodule21Component,
              },
              {
                path: '',
                redirectTo: 'submodule21',
                pathMatch: 'full'
              }
            ]
          }
        ]
      },
      {
        path: 'about',
        component: aboutComponent
      }
    ]
    

    Hope it will help you.

    More details https://angular.io/guide/router

    0 讨论(0)
提交回复
热议问题