错误:无法匹配任何路由。网址段:'主页'错误:无法匹配任何路由。网址细分:'主页'

时间:2017-06-12 13:07:04

标签: angular typescript angular-cli angular-routing

我的app模块路由器模块:

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>

我收到错误“无法匹配任何路由.URL段:'主页'”。 我使用的是最新版本的angular cli,我的<base href="/">标记内有<head>。对此有何解决方案?

1 个答案:

答案 0 :(得分:1)

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

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

同样在你的模块中。改为:

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

其中forChild是导出const

相关问题