在Angular中似乎无需导出声明

时间:2018-06-22 09:52:26

标签: angular ng-modules

在讨论导出声明的Angular NgModule FAQ中,它说:“ 如果不导出声明的类,它将保持私有状态,仅对在此NgModule中声明的其他组件可见 ”,但这似乎不正确。

因为通过live exampleAngular Routes tutorial,我仍然可以访问所有其他组件中的 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)时看不到错误,并且一切正常。

1 个答案:

答案 0 :(得分:0)

  

导出可声明的类,说明其他NgModule中的组件是   能够在其模板中引用

导出定义模板渲染的范围,并且路由器使用ComponentFactory初始化组件。 至于HeroListComponent引用的所有组件都是由AppRoutingModule声明/导入的,那将是可行的。