角2-子模块路由和嵌套 < 路由器出口 >

我正在寻找一个解决方案与角度2的情况下解释如下:

enter image description here

在这个场景中,top-nav 包含到加载子模块的链接,subnav 包含更新子模块内容的链接。

URL 应该映射为:

  • /home = > 在主组件路由器出口加载主页
  • /submodule = > 加载主组件路由器插座中的子模块,默认情况下应该显示子模块的主页和子导航栏
  • 子模块/Feature = > 加载子模块路由器插座内的功能

应用程序模块(和应用程序组件)包含一个顶部导航栏,用于导航到不同的子模块,应用程序组件模板可以如下所示

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

但复杂性在于。我需要我的子模块有一个类似的布局与第二级导航栏和自己的路由器插座加载自己的组件。

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

我尝试了所有的选项,到处搜索,但是没有找到一个解决方案,在子模块中有一个默认的模板(如应用程序组件)与路由器插座,也加载子模块的内容在内部路由器插座没有丢失的子导航。

如果有任何意见和建议,我都会很感激

92028 次浏览

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

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"},
]
},
];

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]
}
]),
...

This would work In Your Template.

<h2>First Component</h2>


<nav>
<ul>
<li><a routerLink="child-a">Child A</a></li>
<li><a routerLink="child-b">Child B</a></li>
</ul>
</nav>


<router-outlet></router-outlet>

In AppRoutingModule.

const routes: Routes = [
{
path: 'first-component',
component: FirstComponent, // this is the component with the <router-outlet> in the template
children: [
{
path: 'child-a', // child route path
component: ChildAComponent, // child route component that the router renders
},
{
path: 'child-b',
component: ChildBComponent, // another child route component that the router renders
},
],
},
];

Read more from https://angular.io/guide/router#nesting-routes