Angular 多模块程序不能正常工作
我有一个多模块的程序。路由模块是AppModule,子模块是AdminModule、EmployeeModule和HomeModule。对于每个模块,我都创建了一个具有相同名称的组件 AppModule的外观如下:Angular 多模块程序不能正常工作,angular,module,multi-module,Angular,Module,Multi Module,我有一个多模块的程序。路由模块是AppModule,子模块是AdminModule、EmployeeModule和HomeModule。对于每个模块,我都创建了一个具有相同名称的组件 AppModule的外观如下: import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { RouterModule, Routes, NoPreloadi
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更改为,但我没有看到employee.component.html加载。我应该怎么做才能修复此问题?请注意,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 { }
不要像在Angular style guide(角度样式指南)中那样使用名称相同的组件:选择器必须是唯一的。