这是我的 Angular2 应用程序结构:
这是我的代码的一部分。以下是module
Angular2 应用程序的主要部分,它导入了它的路由规则和一个子模块 ( EdgeModule
),并使用了与某些页面相关的一些组件。
app.module.ts
@NgModule({
declarations: [
AppComponent,
PageNotFoundComponent,
LoginComponent
],
imports: [
...
appRouting,
EdgeModule
],
providers: [
appRoutingProviders,
LoginService
],
bootstrap: [AppComponent]
})
export class AppModule {
}
这是主模块的路由规则。它具有登录页面和未找到页面的路径。
app.routing.ts
const appRoutes: Routes = [
{ path: 'login', component: LoginComponent },
{ path: '**', component: PageNotFoundComponent }
];
export const appRoutingProviders: any[] = [];
export const appRouting = RouterModule.forRoot(appRoutes, { useHash: true });
这是EdgeModule
声明它使用的组件并导入它自己的路由规则和 2 个子模块(FirstSectionModule
和SecondSectionModule
)。
边缘模块.ts
@NgModule({
declarations: [
EdgeComponent,
SidebarComponent,
TopbarComponent
],
imports: [
...
edgeRouting,
FirstSectionModule,
SecondSectionModule
],
providers: [
AuthGuard
]
})
export class EdgeModule {
}
如您所见,这是加载顶栏和侧边栏组件的模块的路由规则。
边缘路由.ts
Paths['edgePaths'] = {
firstSection: 'firstSection',
secondSection: 'secondSection'
};
const appRoutes: Routes = [
{ path: '', component: EdgeComponent,
canActivate: [AuthGuard],
children: [
{ path: Paths.edgePaths.firstSection, loadChildren: '../somepath/first-section.module#FirstModule' },
{ path: Paths.edgePaths.secondSection, loadChildren: '../someotherpath/second-section.module#SecondModule' },
{ path: '', redirectTo: edgePaths.dashboard, pathMatch: 'full' }
]
}
];
export const edgeRouting = RouterModule.forChild(appRoutes);
最后,这是两个子模块之一,具有其组件并导入其路由规则。
第一部分.module.ts
@NgModule({
declarations: [
FirstSectionComponent,
SomeComponent
],
imports: [
...
firstSectionRouting
],
providers: [
AuthGuard,
]
})
export class FirstSectionModule {
}
这些是页面(组件)的路由规则FirstSectionModule
第一部分.routing.ts
Paths['firstSectionPaths'] = {
someSubPage: 'some-sub-page',
someOtherSubPage: 'some-other-sub-page'
};
const appRoutes: Routes = [
{
path: '',
children: [
{ path: Paths.firstSectionPaths.someSubPage, component: someSubPageComponent},
{ path: Paths.firstSectionPaths.someOtherSubPage, component: someOtherSubPageComponent},
{ path: '', component: AnagraficheComponent }
]
}
];
export const firstSectionRouting = RouterModule.forChild(appRoutes);
second-section.module.ts
和second-section.routing.ts
文件几乎相同。
当我运行应用程序时,首先加载的是与 相关的页面FirstSectionComponent
,没有侧边栏也没有顶栏。
你能告诉我我的代码有什么问题吗?控制台中没有错误。