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

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

我的应用程序模块路由器模块:

RouterModule.forRoot([
{path: 'home' , component: HomeComponent},
{path:'edit/:id', component:EditComponent},
{path: 'messages' , component: MessageComponent},
{path: '', redirectTo:'home',pathMatch:'full'},
])

我的第二个模块:

RouterModule.forChild
([
{
path:'first',
component:FirstComponent,
outlet:'admin'
},
{
path:"second",
component:SecondComponent
},
{
path:"admin",
component:AdminComponent,
outlet:'admin'
}
])

我的app.component.html:

<div >
  <router-outlet>
      <div class="col-md-offset-3 col-md-1">
           <router-outlet name="admin"></router-outlet>
      </div>
  </router-outlet>
</div>

因此,每当我像这样在 .ts 文件中使用路由器时,一切正常,

this.router.navigate([{outlets: { admin: ['admin']}}]);

但是当我尝试在我的 html 中使用它时,像这样使用 routerLink:

<a [routerLink]="[{outlets: { admin: ['admin']}}]">routr</a>

我遇到错误 "Cannot match any routes. URL Segment: 'home'"。 我正在使用最新版本的 angular cli,我的 <head> 标签中有 <base href="/">。有什么解决办法吗?

我认为您应该将 HomeComponent 添加到您的 RouterModule.forChild,如下所示

RouterModule.forChild
([
{
path:'',
redirectTo: '/home',
pathMatch: 'full'
}
])

同样在你的 module.ts 中更改为:

@NgModule({ 进口: RouterModule.forRoot(forChild)

其中 forChild 是一个导出常量