对于在拆分模块后检查您的路由的人有此错误,以下事情发生在我身上:
public-routing.module.ts:
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: '**', redirectTo: 'home' } // ← This was my mistake
{ path: 'home', component: HomeComponent },
{ path: 'privacy-policy', component: PrivacyPolicyComponent },
{ path: 'credits', component: CreditsComponent },
{ path: 'contact', component: ContactComponent },
{ path: 'news', component: NewsComponent },
{ path: 'presentation', component: PresentationComponent }
]
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class PublicRoutingModule { }
app-routing.module.ts:
const routes: Routes = [
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
移至{ path: '**', redirectTo: 'home' }
您的AppRoutingModule:
public-routing.module.ts:
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'home', component: HomeComponent },
{ path: 'privacy-policy', component: PrivacyPolicyComponent },
{ path: 'credits', component: CreditsComponent },
{ path: 'contact', component: ContactComponent },
{ path: 'news', component: NewsComponent },
{ path: 'presentation', component: PresentationComponent }
]
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class PublicRoutingModule { }
app-routing.module.ts:
const routes: Routes = [
{ path: '**', redirectTo: 'home' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
[routerLink]='[/home']
吗?您使用的是哪个Angular2版本和路由器版本?