使用新的角度懒惰路由策略,我希望以下情况能起作用,但是,它始终假定路由不存在。
在我的应用程序中有一个子目录,称为/ admin,具有我想要呈现的各种视图,例如/ admin / settings,/ admin / action-centre等。
通常,路由为/ admin / {route},但是,对于我的策略,我不想将路由移动到app-routing.module.ts中,而是将/ admin的路由包含在admin-routing.module.ts文件。
然后,在app-routing.module.ts中,如果找不到路由,我将对错误视图应用后备'**'。
如果我删除了“ **”后备,则路由有效,但是,该应用始终假定我的路由在admin中不存在,并且失败。
admin-routing.module.ts
<servlet-container name="default" default-encoding="UTF-8">
admin.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { SettingsComponent } from 'app/admin/settings/settings.component';
const routes: Routes = [
{
path: 'settings',
component: SettingsComponent,
canActivate: [AuthRequiredGuard]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class AdminRoutingModule { }
app-routing.module.ts
import { NgModule } from '@angular/core';
import { AdminRoutingModule } from './admin-routing.module';
import { CommonModule } from '@angular/common';
import { SettingsComponent } from './settings/settings.component';
@NgModule({
declarations: [
SettingsComponent
],
imports: [
AdminRoutingModule,
CommonModule,
],
providers: [],
entryComponents: [],
})
export class AdminModule { }
app.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AuthRequiredGuard } from './guards/auth-required.guard';
import { RouteManagerComponent } from './route-manager/route-manager.component';
import { ErrorsComponent } from './errors/errors.component';
const routes: Routes = [
{
path: '',
redirectTo: 'redirect',
pathMatch: 'full'
},
{
path: 'redirect',
component: RouteManagerComponent,
canActivate: [AuthRequiredGuard]
},
{
path: '**',
component: ErrorsComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(routes,
{
paramsInheritanceStrategy: 'always',
enableTracing: true
})],
exports: [RouterModule]
})
export class AppRoutingModule { }