Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Angular中,导出声明似乎没有必要_Angular_Ng Modules - Fatal编程技术网

在Angular中,导出声明似乎没有必要

在Angular中,导出声明似乎没有必要,angular,ng-modules,Angular,Ng Modules,从导出可声明性的角度来看,它说“如果不导出可声明类,它将保持私有状态,仅对此模块中声明的其他组件可见”,但这似乎不正确。 因为从中,我仍然可以访问所有其他组件中的HeroListComponent,如中的AppComponent CrisListComponent等。 但是很明显,在HeroesModule中,我声明了HeroListComponent,但我没有导出它。看起来,即使我不导出声明文件(管道、组件、指令),我仍然可以从声明它们的模块外部访问这些声明文件。那么为什么会发生这种情况呢

从导出可声明性的角度来看,它说“如果不导出可声明类,它将保持私有状态,仅对此模块中声明的其他组件可见”,但这似乎不正确。

因为从中,我仍然可以访问所有其他组件中的HeroListComponent,如中的AppComponent CrisListComponent等。

但是很明显,在HeroesModule中,我声明了HeroListComponent,但我没有导出它。看起来,即使我不导出声明文件(管道、组件、指令),我仍然可以从声明它们的模块外部访问这些声明文件。那么为什么会发生这种情况呢?

以下是我如何从HeroesModule外部访问HeroListComponent:
(此文件为app routing.module.ts

从'@angular/core'导入{NgModule};
从'@angular/router'导入{RouterModule,Routes};
从“./compose message.component”导入{ComposeMessageComponent};
从“./未找到.组件”导入{PageNotFoundComponent};
从“.”导入{CanDeactivateGuard}/可以停用guard.service“;
从“./auth-guard.service”导入{AuthGuard};
从“/选择性预加载策略”导入{SelectivePreloadingStrategy};
从“./heros/hero list.component”导入{HeroListComponent};
施工许可:路线=[
{
路径:“撰写”,
组件:ComposeMessageComponent,
出口:“弹出窗口”
},
{
路径:“测试”,
组件:HeroListComponent
},
{
路径:“admin”,
loadChildren:'app/admin/admin.module#AdminModule',
canLoad:[AuthGuard]
},
{
路径:“危机中心”,
loadChildren:“应用程序/危机中心/危机中心.模块#CrisCenter模块”,
数据:{preload:true}
},
{路径:'',重定向到:'/superheros',路径匹配:'full'},
{路径:'**',组件:PageNotFoundComponent}
];
@NGD模块({
进口:[
RouterModule.forRoot(
批准,
{
//enableTracing:true,//
导出其他模块中的组件所属的可声明类
能够在其模板中引用

导出定义模板呈现的范围,路由器使用ComponentFactory初始化组件。 只要HeroListComponent引用的所有组件都由AppRoutingModule声明/导入,这就可以了

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 { }