简体   繁体   English

角嵌套路由由主路由器出口移除

[英]Angular nested routing is removed by the main router outlet

I have a app in which I have a header and a sidebar. 我有一个应用程序,其中有标题和侧边栏。 It looks like this: 看起来像这样:

#app.component.html
<mat-drawer-container class="sidenav-container">

    <app-side-nav></app-side-nav>

  <mat-drawer-content>
    <app-header></app-header>
    <main>
      <router-outlet></router-outlet>
    </main>
  </mat-drawer-content>
</mat-drawer-container>

And routing configuration is: 路由配置为:

const routes: Routes = [
  { path: 'identity', loadChildren: './identity-registry/identity-registry.module#IdentityRegistryModule' }
];

Now upon clicking on identity the identity module will load a nested nav menu within it. 现在,单击身份后, identity module将在其中加载嵌套的导航菜单。 The identity module has 3 component (IdentityRegistryComponent,MyIdentityComponent, UsersComponent) and it has its own routing configuration. 身份模块具有3个组件(IdentityRegistryComponent,MyIdentityComponent,UsersComponent),并且具有自己的路由配置。

const routes: Routes = [
  {
    path: '',
    component: IdentityRegistryComponent
  },
  {
    path: 'my-identity',
    component: MyIdentityComponent
  },
  {
    path: 'users',
    component: UsersComponent
  }
];

and the nested route looks like this: 嵌套的路由如下所示:

###IdentityRegistryComponent

<nav mat-tab-nav-bar>
  <a mat-tab-link [routerLink]="['./my-identity']">My Identity</a>

  <a mat-tab-link [routerLink]="['./users']">Users</a>
</nav>
<router-outlet></router-outlet>

But unfortunately, whenever I click on identity, its load the IdentityRegistryComponent properly. 但是不幸的是,每当我单击身份时,它都会正确加载IdentityRegistryComponent But click on my-identity , disappear the nested routes and load the respective component only. 但是单击my-identity ,消失嵌套的路由并仅加载相应的组件。 But it should not be like that. 但这不应该是那样。 The nested loop should be there and upon clicking on my-identity , it should load the respective component on router-outlet . 嵌套循环应该在那里,并且在单击my-identity ,应该将相应的组件加载到router-outlet I do not know how to make it working? 我不知道如何使它工作?

Besides, is there anyway that, if i click on identity from the nav, it will load the IdentityRegistryComponent and by default MyIdentityComponent will be loaded in a nested routes zone? 此外,无论如何,如果我从导航中单击身份,它将加载IdentityRegistryComponent并且默认情况下MyIdentityComponent将加载在嵌套的路由区域中?

for better understanding, i have add the git link: testApp 为了更好地理解,我添加了git链接: testApp

If you want to use the router outlet for this module, you need to specify the component for this 'child'. 如果要将路由器插座用于此模块,则需要为此“子级”指定组件。

const routes: Routes = [{
  path: 'identity',
  component: AppComponent,
  loadChildren: './identity-registry/identity-registry.module#IdentityRegistryModule'
}];

If you want all your routes to use the same component you can also define it like so: 如果您希望所有路线都使用相同的组件,则也可以这样定义它:

const routes: Routes = [{
  path: '',
  component: AppComponent,
  children: [{
      path: 'identity',
      component: NestedNavComponent, // Add a component here for nested router-outlets
      loadChildren: './identity-registry/identity-registry.module#IdentityRegistryModule'
  },{
      path: 'another-route',
      loadChildren: './another-route/another-route.module#AnotherRouteModule'
  }]
}];

Update ( See the nested component reference I added ): 更新( 请参阅我添加的嵌套组件参考 ):

Routes work hierarchical and that includes router-outlets in nested routes. 路由是分层的,并且在嵌套路由中包括路由器出口。 The application is built in layers for child routes. 该应用程序内置在子路由的图层中。 If you consider that, you can define your child routes the same way as you designed your ui logic. 如果考虑到这一点,则可以按照设计ui逻辑的相同方式定义子路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM