我有一个多模块程序。路由模块是AppModule,子级是AdminModule,EmployeeModule和HomeModule。对于每个模块,我创建了一个具有相同名称的组件。
AppModule 看起来像这样:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Routes, NoPreloading } from '@angular/router';
import { AppComponent } from './app.component';
import { HomeModule } from './home/home.module';
const routes: Routes = [
{ path: 'Home', loadChildren:'app/home/home.module#HomeModule' },
{ path: 'Employee', loadChildren:'app/employee/employee.module#EmployeeModule' },
{ path: 'Admin', loadChildren:'app/admin/admin.module#AdminModule' },
];
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
HomeModule,
RouterModule.forRoot(routes, { useHash: false, preloadingStrategy: NoPreloading }),
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
这是其他3个模块中每个模块的内部样子:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HomeComponent } from './home/home.component';
import { Routes } from '@angular/router';
const routes: Routes = [
{ path: 'Home', component: HomeComponent },
];
@NgModule({
imports: [
CommonModule
],
declarations: [HomeComponent]
})
export class HomeModule { }
程序加载并正常运行。例如,当我单击 Home 时,URL更改为http://localhost:4200/Home,但看不到employee.component.html加载。我该怎么做才能解决此问题?
答案 0 :(得分:1)
请注意,HomeModule中的路径路由设置为空字符串。这是因为AppModule路由中的路径已经设置为Home,所以HomeModule路由中的该路由已经在Home上下文中。此路由模块中的每条路由都是子路由。
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path: '', component: HomeComponent },
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(routes)
],
declarations: [HomeComponent]
})
export class HomeModule { }