Error: Cannot match any routes. URL Segment: 'homePage'

Error: Cannot match any routes. URL Segment: 'homePage'

我想在单击登录按钮后转到主页,但我一直收到错误消息:

Error: Cannot match any routes. URL Segment: 'homePage'

这是我的路线:

{
  path: 'homePage',
  component: HomePageComponent,
  outlet:'homePage'
},
{ 
  path: 'login',
  component:LoginComponent
},  
{ 
  path: '',
  redirectTo: '/login',
  pathMatch: 'full'
} 

这是要点击的按钮:

<button routerLink="homePage" id="login" md-button>Log in</button>
<router-outlet name="homePage"></router-outlet>

尝试更改

<button routerLink="homePage">

<button routerLink="/homePage"

您必须像这样告诉路由器在您指定的插座内路由:

<button [routerLink]="[{ outlets:{ homePage: ['homePage']} }]">
  Edit
</button>

可以找到更多关于使用命名出口路由的信息 here


正如我在此回答的评论中所述,听起来您只想拥有一个简单的未命名路由器插座。

确保您的应用组件模板中只有一个插座:

<router-outlet></router-outlet>

然后从您的路线中移除出口 属性:

{
    path: 'homePage',
    component: HomePageComponent
}

对于路由器 link 只需使用

<button routerLink="/homePage></button>