Angular 路由子惰性模块
Angular routing child lazy module
我正在使用 Angular 4 和 Lazy/shared 模块的路由。
这里如何使用路径而不在 URL 中附加我的惰性模块名称?
目前我的 partner
组件可通过 http://localhost:4200/#/main/partner
访问,但我想与 URL http://localhost:4200/#/partner
一起使用。没有将 /main
附加到我的组件。
dashboard
、employeelist
和 addemployee
也一样,我想直接用 localhost:4200/#/dashboard
等访问。
下面是我的 App-Routing 和 lazy-Routing 文件。
app.routing.ts
const routes: Routes = [
{ path: '', component: LoginComponent },
{ path: 'emailLogin/forgotpassword', component: ForgotPasswordComponent },
{ path: 'forgotpassword', component: ForgotPasswordComponent },
{ path: 'login', redirectTo: '', component: LoginComponent },
{ path: 'main', loadChildren: './lazy.module#LazyModule' },
{ path: '**', redirectTo: '/main' }
];
export const routing = RouterModule.forRoot(routes, { useHash: true });
lazy.routing.ts
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthguardGuardPartnerUser] },
{ path: 'partner', component: PartnerComponent, canActivate: [AuthguardGuard] },
{ path: 'employeelist', component: EmployeeComponent, canActivate: [AuthguardGuardPartnerUser] },
{ path: 'addemployee', component: AddemployeeComponent, canActivate: [AuthguardGuardPartnerUser] },
{ path: 'newsurvey/:neworcopy', component: NewsurveyComponent, canActivate: [AuthguardGuardAdminPartner] },
// .... Other components
{ path: '404pageNotFound', component: NotfoundComponent },
{ path: '**', redirectTo: '/404pageNotFound' }
];
export const routing: ModuleWithProviders = RouterModule.forChild(routes);
我假设你知道你不能让同一条路线加载两个不同的东西,所以你需要重组。我可以想到 3 种方法。
选项 1
const routes: Routes = [
{ path: '', component: LoginComponent },
{ path: 'emailLogin/forgotpassword', component: ForgotPasswordComponent },
{ path: 'forgotpassword', component: ForgotPasswordComponent },
{ path: 'login', redirectTo: '', component: LoginComponent },
{ path: 'partner', loadChildren: './lazy.module#LazyModule' },
{ path: '**', redirectTo: '/main' }
];
export const routing = RouterModule.forRoot(routes, { useHash: true });
const routes: Routes = [
{ path: '', component: PartnerComponent, canActivate: [AuthguardGuard] },
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthguardGuardPartnerUser] },
{ path: '404pageNotFound', component: NotfoundComponent },
{ path: '**', redirectTo: '/404pageNotFound' }
];
export const routing: ModuleWithProviders = RouterModule.forChild(routes);
这将在 /partner
加载惰性模块,然后由于惰性模块内的第一个路由现在是 '',它将加载合作伙伴组件。但显然这意味着您的仪表板现在是 /partner/dashboard
.
选项 2
或者,将合作伙伴组件移出惰性模块并移至您的应用模块中:
const routes: Routes = [
{ path: '', component: LoginComponent },
{ path: 'emailLogin/forgotpassword', component: ForgotPasswordComponent },
{ path: 'forgotpassword', component: ForgotPasswordComponent },
{ path: 'login', redirectTo: '', component: LoginComponent },
{ path: 'partner', component: PartnerComponent, canActivate: [AuthguardGuard] },
{ path: 'main', loadChildren: './lazy.module#LazyModule' },
{ path: '**', redirectTo: '/main' }
];
export const routing = RouterModule.forRoot(routes, { useHash: true });
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthguardGuardPartnerUser] },
{ path: '404pageNotFound', component: NotfoundComponent },
{ path: '**', redirectTo: '/404pageNotFound' }
];
export const routing: ModuleWithProviders = RouterModule.forChild(routes);
选项 3
或者只是将合作伙伴组件移动到它自己的模块中:
const routes: Routes = [
{ path: '', component: LoginComponent },
{ path: 'emailLogin/forgotpassword', component: ForgotPasswordComponent },
{ path: 'forgotpassword', component: ForgotPasswordComponent },
{ path: 'login', redirectTo: '', component: LoginComponent },
{ path: 'main', loadChildren: './lazy.module#LazyModule' },
{ path: 'partner', loadChildren: './partner.module#PartnerModule' },
{ path: '**', redirectTo: '/main' }
];
export const routing = RouterModule.forRoot(routes, { useHash: true });
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthguardGuardPartnerUser] },
{ path: '404pageNotFound', component: NotfoundComponent },
{ path: '**', redirectTo: '/404pageNotFound' }
];
export const routing: ModuleWithProviders = RouterModule.forChild(routes);
partner.module.ts
const routes: Routes = [
{ path: '', component: PartnerComponent, canActivate: [AuthguardGuard] },
];
export const routing: ModuleWithProviders = RouterModule.forChild(routes);
我怀疑第三个选项可能是最合适的,因为它使您当前的所有路线保持不变,并提供更多的灵活性。
终于想通了,改一下就可以了
{ path: '', loadChildren: './lazy.module#LazyModule' },
{ path: '**', redirectTo: '' }
在 app.routing.ts
中解决了我的问题。
我正在使用 Angular 4 和 Lazy/shared 模块的路由。
这里如何使用路径而不在 URL 中附加我的惰性模块名称?
目前我的 partner
组件可通过 http://localhost:4200/#/main/partner
访问,但我想与 URL http://localhost:4200/#/partner
一起使用。没有将 /main
附加到我的组件。
dashboard
、employeelist
和 addemployee
也一样,我想直接用 localhost:4200/#/dashboard
等访问。
下面是我的 App-Routing 和 lazy-Routing 文件。
app.routing.ts
const routes: Routes = [
{ path: '', component: LoginComponent },
{ path: 'emailLogin/forgotpassword', component: ForgotPasswordComponent },
{ path: 'forgotpassword', component: ForgotPasswordComponent },
{ path: 'login', redirectTo: '', component: LoginComponent },
{ path: 'main', loadChildren: './lazy.module#LazyModule' },
{ path: '**', redirectTo: '/main' }
];
export const routing = RouterModule.forRoot(routes, { useHash: true });
lazy.routing.ts
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthguardGuardPartnerUser] },
{ path: 'partner', component: PartnerComponent, canActivate: [AuthguardGuard] },
{ path: 'employeelist', component: EmployeeComponent, canActivate: [AuthguardGuardPartnerUser] },
{ path: 'addemployee', component: AddemployeeComponent, canActivate: [AuthguardGuardPartnerUser] },
{ path: 'newsurvey/:neworcopy', component: NewsurveyComponent, canActivate: [AuthguardGuardAdminPartner] },
// .... Other components
{ path: '404pageNotFound', component: NotfoundComponent },
{ path: '**', redirectTo: '/404pageNotFound' }
];
export const routing: ModuleWithProviders = RouterModule.forChild(routes);
我假设你知道你不能让同一条路线加载两个不同的东西,所以你需要重组。我可以想到 3 种方法。
选项 1
const routes: Routes = [
{ path: '', component: LoginComponent },
{ path: 'emailLogin/forgotpassword', component: ForgotPasswordComponent },
{ path: 'forgotpassword', component: ForgotPasswordComponent },
{ path: 'login', redirectTo: '', component: LoginComponent },
{ path: 'partner', loadChildren: './lazy.module#LazyModule' },
{ path: '**', redirectTo: '/main' }
];
export const routing = RouterModule.forRoot(routes, { useHash: true });
const routes: Routes = [
{ path: '', component: PartnerComponent, canActivate: [AuthguardGuard] },
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthguardGuardPartnerUser] },
{ path: '404pageNotFound', component: NotfoundComponent },
{ path: '**', redirectTo: '/404pageNotFound' }
];
export const routing: ModuleWithProviders = RouterModule.forChild(routes);
这将在 /partner
加载惰性模块,然后由于惰性模块内的第一个路由现在是 '',它将加载合作伙伴组件。但显然这意味着您的仪表板现在是 /partner/dashboard
.
选项 2
或者,将合作伙伴组件移出惰性模块并移至您的应用模块中:
const routes: Routes = [
{ path: '', component: LoginComponent },
{ path: 'emailLogin/forgotpassword', component: ForgotPasswordComponent },
{ path: 'forgotpassword', component: ForgotPasswordComponent },
{ path: 'login', redirectTo: '', component: LoginComponent },
{ path: 'partner', component: PartnerComponent, canActivate: [AuthguardGuard] },
{ path: 'main', loadChildren: './lazy.module#LazyModule' },
{ path: '**', redirectTo: '/main' }
];
export const routing = RouterModule.forRoot(routes, { useHash: true });
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthguardGuardPartnerUser] },
{ path: '404pageNotFound', component: NotfoundComponent },
{ path: '**', redirectTo: '/404pageNotFound' }
];
export const routing: ModuleWithProviders = RouterModule.forChild(routes);
选项 3
或者只是将合作伙伴组件移动到它自己的模块中:
const routes: Routes = [
{ path: '', component: LoginComponent },
{ path: 'emailLogin/forgotpassword', component: ForgotPasswordComponent },
{ path: 'forgotpassword', component: ForgotPasswordComponent },
{ path: 'login', redirectTo: '', component: LoginComponent },
{ path: 'main', loadChildren: './lazy.module#LazyModule' },
{ path: 'partner', loadChildren: './partner.module#PartnerModule' },
{ path: '**', redirectTo: '/main' }
];
export const routing = RouterModule.forRoot(routes, { useHash: true });
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthguardGuardPartnerUser] },
{ path: '404pageNotFound', component: NotfoundComponent },
{ path: '**', redirectTo: '/404pageNotFound' }
];
export const routing: ModuleWithProviders = RouterModule.forChild(routes);
partner.module.ts
const routes: Routes = [
{ path: '', component: PartnerComponent, canActivate: [AuthguardGuard] },
];
export const routing: ModuleWithProviders = RouterModule.forChild(routes);
我怀疑第三个选项可能是最合适的,因为它使您当前的所有路线保持不变,并提供更多的灵活性。
终于想通了,改一下就可以了
{ path: '', loadChildren: './lazy.module#LazyModule' },
{ path: '**', redirectTo: '' }
在 app.routing.ts
中解决了我的问题。