我有一个简单的路由器:
const routes = [
{
path : "",
component: AuthLayoutComponent
},
{
path : "**",
component: PageNotFoundComponent
}
];
export const routing = RouterModule.forRoot(routes);
我一直看到PageNotFoundComponent。如果删除“**”路由器,那么我看到一个错误:
Error: Cannot match any routes: ''
模块:
@NgModule({
declarations: [
MainComponent,
components
],
imports : [
routing,
BrowserModule
],
providers : [
appRoutingProviders
],
bootstrap : [MainComponent]
})
答案 0 :(得分:2)
const routes = [
{
path : "",
pathMatch: 'full'; <<<<==== added
component: AuthLayoutComponent
},
{
path : "**",
component: PageNotFoundComponent
}
];
export const routing = RouterModule.forRoot(routes);
如果没有pathMatch: 'full'
,路由器会在''
匹配后继续搜索路径为空的子路由。