在哪里声明 Angular catch all (Not found) 路由
Where to declare Angular catch all (Not found) route
我有一个主 app.routing.module.ts
是这样的:
const routes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'login', component: LoginComponent },
{ path: '', redirectTo: '/home', pathMatch: 'full' }
];
@NgModule({
imports: [
RouterModule.forRoot(routes),
],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
然后是几个子路由模块:
const routes: Routes = [
{ path: 'AbcRoute1', component: AbcRoute1Component },
{ path: 'AbcRoute2', component: AbcRoute2Component }
];
@NgModule({
imports: [ RouterModule.forChild(routes) ],
exports: [ RouterModule ]
})
export class AbcRoutingModule {}
const routes: Routes = [
{ path: 'DefRoute1', component: DefRoute1Component },
{ path: 'DefRoute2', component: DefRoute2Component }
];
@NgModule({
imports: [ RouterModule.forChild(routes) ],
exports: [ RouterModule ]
})
export class DefRoutingModule {}
我将此称为包罗万象的路线:
{ path: '**', redirectTo: '/home', pathMatch: 'full' }
如果我把它放在 AppRoutingModule
中,那么如果它不匹配 AppRoutingModule
中定义的任何路由,它就会启动。例如,我无法转到 DefRoutingModule
中定义的 https://myapp/DefRoute1
如果我把它放在 AbcRoutingModule
中,那么所有本地路由都可以工作,但 DefRoutingModule
中的内容不起作用。
我应该把它放在哪里,以便它只在我的所有路由模块都无法匹配 url 时才会匹配?
确保 AppRoutingModule
(根路由模块)在模块导入中最后声明。然后在那里声明捕获所有通配符。文档 link:https://angular.io/guide/router#routing-module-order
@NgModule({
declarations: [
//...
],
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
HttpClientModule,
// all other modules including any child routing modules
// Last module
AppRoutingModule
],
providers: [
//..
],
bootstrap: [
//..
]
})
export class AppModule { }
我有一个主 app.routing.module.ts
是这样的:
const routes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'login', component: LoginComponent },
{ path: '', redirectTo: '/home', pathMatch: 'full' }
];
@NgModule({
imports: [
RouterModule.forRoot(routes),
],
exports: [ RouterModule ]
})
export class AppRoutingModule {}
然后是几个子路由模块:
const routes: Routes = [
{ path: 'AbcRoute1', component: AbcRoute1Component },
{ path: 'AbcRoute2', component: AbcRoute2Component }
];
@NgModule({
imports: [ RouterModule.forChild(routes) ],
exports: [ RouterModule ]
})
export class AbcRoutingModule {}
const routes: Routes = [
{ path: 'DefRoute1', component: DefRoute1Component },
{ path: 'DefRoute2', component: DefRoute2Component }
];
@NgModule({
imports: [ RouterModule.forChild(routes) ],
exports: [ RouterModule ]
})
export class DefRoutingModule {}
我将此称为包罗万象的路线:
{ path: '**', redirectTo: '/home', pathMatch: 'full' }
如果我把它放在 AppRoutingModule
中,那么如果它不匹配 AppRoutingModule
中定义的任何路由,它就会启动。例如,我无法转到 DefRoutingModule
https://myapp/DefRoute1
如果我把它放在 AbcRoutingModule
中,那么所有本地路由都可以工作,但 DefRoutingModule
中的内容不起作用。
我应该把它放在哪里,以便它只在我的所有路由模块都无法匹配 url 时才会匹配?
确保 AppRoutingModule
(根路由模块)在模块导入中最后声明。然后在那里声明捕获所有通配符。文档 link:https://angular.io/guide/router#routing-module-order
@NgModule({
declarations: [
//...
],
imports: [
BrowserModule,
FormsModule,
ReactiveFormsModule,
HttpClientModule,
// all other modules including any child routing modules
// Last module
AppRoutingModule
],
providers: [
//..
],
bootstrap: [
//..
]
})
export class AppModule { }