来自ASP.NET Core JavaScript 服务的模板带有一个名为AppModule
. 由于我的应用程序分为两个逻辑区域,因此为它们使用两个模块(AreaA、AreaB)似乎是个好主意。我的想法是将两者都导入AppModule
,包括像 Pipes 这样的共享资源(这可能会在这里造成麻烦)。
因此,出于测试目的,我创建了一个名为ModuleA
import { HomeComponent } from './home/home.component';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { UniversalModule } from 'angular2-universal';
@NgModule({
declarations: [
HomeComponent
],
imports: [
UniversalModule, // Must be first import. This automatically imports BrowserModule, HttpModule, and JsonpModule too.
RouterModule.forChild([
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
])
]
})
export class ModuleAModule {}
AppModule
它是这样导入的
import { ModuleAModule } from './module-a/module-a.module';
import { NavMenuComponent } from './shared/navmenu/navmenu.component';
import { AppComponent } from './shared/app/app.component';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { UniversalModule } from 'angular2-universal';
@NgModule({
bootstrap: [ AppComponent ],
declarations: [
AppComponent,
NavMenuComponent
],
imports: [
UniversalModule, // Must be first import. This automatically imports BrowserModule, HttpModule, and JsonpModule too.
ModuleAModule
]
})
export class AppModule {}
这给了我一个例外
异常:调用节点模块失败并出现错误:错误:模板解析错误:'router-outlet' 不是已知元素:1. 如果'router-outlet' 是 Angular 组件,则验证它是否是该模块的一部分。2. 如果 'router-outlet' 是 Web 组件,则将“CUSTOM_ELEMENTS_SCHEMA”添加到该组件的 '@NgModule.schemas' 以禁止显示此消息。
该<router-outlet>
标记用于app.component
作为主要内容的占位符。但是当我像这样在主应用程序模块中设置路由时它可以工作
imports: [
UniversalModule, // Must be first import. This automatically imports BrowserModule, HttpModule, and JsonpModule too.
ParentAreaModule,
RouterModule.forRoot([
{path: 'home',component:HomeComponent}
])
]
这将迫使我在app.module
. 它需要跨不同模块导入我的所有组件,这对我来说似乎是一团糟。我想在子模块本身中设置路线。最好的解决方案是为每个模块自动添加前缀(如第一个模块的 module-a)。