[英]Handling 'Routes' of Imported Modules on Angular
I'm having some difficulties in understanding/using the routes
of an imported module
. 我在理解/使用导入
module
的routes
时遇到一些困难。 The problem occurs on routes
that rely on secondary outlets or named outlets . 在依赖于辅助出口或命名出口的
routes
上会出现问题。
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.html
和entity-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
引用命名出口的链接已损坏**。
This test application on stackblitz illustrates the issue. 这个关于stackblitz的测试应用程序说明了这个问题。
entity-b.component
. entity-b.component
。 entity-b-detail.component
. entity-b-detail.component
。 entity-a.component
. 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
。 Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'entity-a/a'
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
并使用其选择器显示其内容。 [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. 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.