从关于导出可声明的 Angular NgModule 常见问题解答中,它说“如果你不导出可声明的类,它会保持私有,只对在这个 NgModule 中声明的其他组件可见”,但这似乎不正确。
因为从这个Angular Routes 教程的实时示例中,我仍然可以在所有其他组件中访问HeroListComponent ,例如在AppComponent中、在
CrisisListComponent 中等。
但很明显,在HeroesModule中,我声明了 HeroListComponent
,但我没有导出它。似乎即使我不导出可声明对象(管道、组件、指令),我仍然可以从声明它们的模块外部访问这些可声明对象。那么为什么会这样呢?
下面是我从 HeroesModule 外部访问 HeroListComponent 的方法:(
这个文件是app-routing.module.ts)
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { ComposeMessageComponent } from './compose-message.component';
import { PageNotFoundComponent } from './not-found.component';
import { CanDeactivateGuard } from './can-deactivate-guard.service';
import { AuthGuard } from './auth-guard.service';
import { SelectivePreloadingStrategy } from './selective-preloading-strategy';
import { HeroListComponent } from './heroes/hero-list.component';
const appRoutes: Routes = [
{
path: 'compose',
component: ComposeMessageComponent,
outlet: 'popup'
},
{
path: 'test',
component: HeroListComponent
},
{
path: 'admin',
loadChildren: 'app/admin/admin.module#AdminModule',
canLoad: [AuthGuard]
},
{
path: 'crisis-center',
loadChildren: 'app/crisis-center/crisis-center.module#CrisisCenterModule',
data: { preload: true }
},
{ path: '', redirectTo: '/superheroes', pathMatch: 'full' },
{ path: '**', component: PageNotFoundComponent }
];
@NgModule({
imports: [
RouterModule.forRoot(
appRoutes,
{
// enableTracing: true, // <-- debugging purposes only
preloadingStrategy: SelectivePreloadingStrategy,
}
)
],
exports: [
RouterModule
],
providers: [
CanDeactivateGuard,
SelectivePreloadingStrategy
]
})
export class AppRoutingModule { }
导航到路径“test”(http://localhost:4200/test)时没有看到任何错误,并且一切正常。