Angular“错误:无法匹配任何路由。使用[routerLink]时使用“ URL段”,但可与this.router.navigate

问题描述 投票:0回答:2

我在应用程序中有一个主要出口和一个命名出口,定义如下:

<router-outlet></router-outlet>
<router-outlet name="center"></router-outlet>

我在我的应用程序路由中定义了与此中心组件一起使用的路由:

const routes: Routes = [
{ path: 'login', component: LoginComponent, outlet: 'center' },   
{ path: 'home',  component: HomeComponent },
{ path: 'car',       component: BaseComponent },
{ path: 'car/:id',   component: CarComponent },
{ path: 'gi/:otherId',component:GIComponent, outlet: 'center' },
{ path: '', pathMatch: 'full', redirectTo: 'home' }];

我正在Car.component.html中定义一个链接,以将GI组件路由到指定的出口。但是这个链接给我一个错误:

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

但是,我能够使用router.navigate()创建一个函数来执行此导航:

<a [routerLink]="" (click)="routeGI()" > Gi Link  </a>

...以及我的Car.Component.ts文件中的关联函数:

routeGI() { this.router.navigate([{ outlets: {center: ['gi', this.myObject.id]  }}]);

因此,routeGI()函数的作用就像一个超级按钮,但是使用[routerLink]从html进行导航时,出现以下异常:

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 
'car/2112'
Error: Cannot match any routes. URL Segment: 'car/2112'
at ApplyRedirects.push../node_modules/@angular/router/fesm5/router.js.ApplyRedirects.noMatchError 
(router.js:2464)
at CatchSubscriber.selector (router.js:2445)
at CatchSubscriber.push../node_modules/rxjs/_esm5/internal/operators/catchError.js.CatchSubscriber.error (catchError.js:34)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._error (Subscriber.js:79)

这些到我命名的出口的链接都行不通吗?

angular angular-routing angular-routerlink
2个回答
0
投票

此更具体的问题由一个显示问题的StackBlitz示例回答:Routing to a named outlet from links in a navigation component fails only when using [routerLink]


0
投票

像波纹管一样在出口前添加空字符串

[routerLink]="['', { outlets: { center: ['gi', '10'] } }]"
© www.soinside.com 2019 - 2024. All rights reserved.