Routing to a named outlet from links in a navigation component fails only when using [routerLink]

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-25 08:19:05

问题


My application receives an error when I try to route to a named outlet using this link in html:

<a [routerLink]=.../> 

But the same route works using this.router.navigate from a ts handler function.

The following StackBlitz shows the issue:StackBlitzNavErrorExample

Click on "CarLink" which shows a simple navigation page, then notice how the button "Change" works but the link "GI Link" does not. But the routing looks the same for me. I want to use [routerLink]= so I can also use routerLinkActive="myClass" to hightlight the link selected.

I receive the following error even though I am not changing the route for the primary outlet:

ERROR
Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'car/9'
Error: Cannot match any routes. URL Segment: 'car/9'

回答1:


Since you have configured routes in root level, you have to move your named router outlet to 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>

ForkedExample




回答2:


change to this in the html

<a [routerLink]="['',{ outlets: { center: ['gi', 10] } }]" > GI Link </a>


来源:https://stackoverflow.com/questions/58117105/routing-to-a-named-outlet-from-links-in-a-navigation-component-fails-only-when-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!