简体   繁体   English

在Angular上处理导入模块的“路线”

[英]Handling 'Routes' of Imported Modules on Angular

I'm having some difficulties in understanding/using the routes of an imported module . 我在理解/使用导入moduleroutes时遇到一些困难。 The problem occurs on routes that rely on secondary outlets or named outlets . 在依赖于辅助出口命名出口的 routes上会出现问题。

The Problem 问题

Suppose I have two modules entity-a and entity-b , and that entity-a imports entity-b , using its content via the selector app-entity-b on entity-a.component.html . 假设我有两个模块entity-a entity-a.component.htmlentity-b ,并且那个entity-a entity-a.component.html通过entity-a.component.html上的选择器 app-entity-b使用其内容导入entity-b entity-a.component.html

The content of entity-b will show perfectly on entity-a , however, ** entity-b links that refer to named outlets became broken**. entity-b的内容将在entity-a -a上完美显示,但是,** entity-b引用命名出口的链接已损坏**。

Application Test 应用测试

This test application on stackblitz illustrates the issue. 这个关于stackblitz的测试应用程序说明了这个问题。

Routes are working within the module 路由在模块内起作用

  • Click on EntityB to open entity-b.component . 单击EntityB以打开entity-b.component
  • Click on Entity B details to open entity-b-detail.component . 单击实体B的详细信息以打开entity-b-detail.component

Routes are broken once the module is imported 导入模块后路由中断

  • Click on EntityA to open entity-a.component . 单击EntityA以打开entity-a.component
  • entity-a.componenet is importing entity-b.module and showing entity-b.component that contains the link to entity-b-detail.component . entity-a.componenet被导入entity-b.module和表示entity-b.component包含链接到entity-b-detail.component
  • Click on Entity B details and get an error: 单击实体B的详细信息,并得到一个错误:
    • Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'entity-a/a'

Things to take into consideration 要考虑的事情

  • Modules entity-a and entity-b are being lazily loaded. entity-a -a和entity-b模块正在延迟加载。
  • entity-a is importing component entity-b and using its selector to show its content. entity-a导入组件entity-b并使用其选择器显示其内容。
  • If the [routerLink] makes reference to entity-b path before calling the outlets a redirect occurs to entity-b component, showing the detail . 如果[routerLink]在调用outlets之前引用了entity-b路径,则会发生重定向entity-b组件,显示detail However, this isn't the expected behavior. 但是,这不是预期的行为。

Relevant code parts 相关代码部分

app.module

const appRoutes: Routes = [
  {
    path: '',
    component: MainComponent
  }
];

const childRoutes: Routes = [
  {
    path: 'entity-a',
    loadChildren: './entities/entity-a/entity-a.module#EntityAModule'
  },
  {
    path: 'entity-b',
    loadChildren: './entities/entity-b/entity-b.module#EntityBModule'
  },
];

const ROUTES = [...childRoutes, ...appRoutes];
@NgModule({
  imports:      [
    BrowserModule,
    FormsModule,
    RouterModule.forRoot(appRoutes),
    RouterModule.forChild(childRoutes)
    ],
  declarations: [ AppComponent, MainComponent, NavComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

entity-a.module

const routeA: Routes = [
  {
    path: 'a',
    component: EntityAComponent
  }
];

@NgModule({
  imports: [
    CommonModule,
    EntityBModule,
    RouterModule.forChild(routeA)
  ],
  declarations: [EntityAComponent],
  exports: [EntityAComponent]
})
export class EntityAModule { }

entity-a.component.html

<h3>Entity A</h3>
<p>
Entity A Content
</p>

<div>
  <em>Importing 'Entity B'</em>
  <app-entity-b></app-entity-b>
</div>

entity-b.module

const routes: Routes = [
  { 
    path: 'b',
    component: EntityBComponent,
    children: [
      {
        path: 'detail',
        component: EntityBDetailComponent,
        outlet: 'details',
      },
    ]
  },

];

@NgModule({
  imports: [
    RouterModule.forChild(routes),
    CommonModule
  ],
  declarations: [
    EntityBComponent,
    EntityBDetailComponent
  ],
  exports: [
    EntityBComponent,
    EntityBDetailComponent
  ]
})
export class EntityBModule { }

entity-b.component.html

<h3>Entity B</h3>
<ul>
<li><a [routerLink]="[ { outlets: { details: 'detail' } }]">
  Entity B details
</a></li>
</ul>
<router-outlet name="details"></router-outlet>

The routing for named outlets work in combination with other routes. 命名出口的路由与其他路由结合使用。 When you try to navigate to entity-b-detail from entity-a, it goes to the route entity-a/a/(details:detail) , and displays the result in named outlet. 当您尝试从entity-a导航到entity-b-detail时,它会转到entity-a/a/(details:detail)路线,并在命名的插座中显示结果。 Since it doesn't find any match, it throws the error. 由于找不到任何匹配项,因此将引发错误。

I've forked your code and made changes here . 我已经分叉了您的代码,并在此处进行了更改。

The only relevant change was on EntityModule . 唯一相关的更改是在EntityModule routeA must hold a reference for path a/detail . routeA必须保留路径a/detail的引用。

entity-a.module

const routeA: Routes = [
  {
    path: 'a',
    component: EntityAComponent,
     children: [
      {
        path: 'detail',
        component: EntityBDetailComponent,
        outlet: 'details',
      },
    ]
  }
];

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

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