angular - 仅在使用 [routerLink] 时,从导航组件中的链接路由到命名出口失败
问题描述
当我尝试使用 html 中的此链接路由到命名插座时,我的应用程序收到错误:
<a [routerLink]=.../>
但是使用 ts 处理程序函数中的 this.router.navigate 可以使用相同的路由。
以下 StackBlitz 显示了该问题:StackBlitzNavErrorExample
单击显示简单导航页面的“CarLink”,然后注意“更改”按钮如何工作,但“GI Link”链接没有。但是路由对我来说看起来一样。我想使用 [routerLink]= 所以我也可以使用 routerLinkActive="myClass" 来突出显示选定的链接。
即使我没有更改主插座的路线,我也会收到以下错误:
ERROR
Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'car/9'
Error: Cannot match any routes. URL Segment: 'car/9'
解决方案
由于您已在根级别配置路由,因此您必须将命名的路由器出口移动到 app.component.html
app.module.ts
const routes: Routes = [
{ path: 'car/:id', component: CarComponent},
{ path: 'gi/:otherId', component: GIComponent, outlet: 'center'}
]
app.component.html
<a [routerLink]="['car', '9']" > Car Link </a>
<router-outlet></router-outlet>
<router-outlet name="center"></router-outlet>
推荐阅读
- c - 用 Void 函数填充结构数组
- github - How to Achieve New File Template on GitHub Using Query String Args?
- python - 无法确定程序提前终止的原因
- r - how to match two column of one data frame with one column of other data frame
- python - Updating MYSQL from daily CSV file - Skip already imported
- laravel - What would be an API end point to retrieve all customer's review of a product and all reviews of a specific customer
- reflection - Kotlin reflection change instance and all members that use the instance
- qt - 如何修改 QtPurchasing.jar 文件?
- c# - 如何将值从一个类传递到另一个 c# Grasshopper
- reactjs - 我的 RCTWebrtcDemo 在启用远程调试器时工作正常,但在禁用远程调试 js 时不起作用