为什么使用router-outlet访问其他模块的组件不需要添加导出

Chu*_* Li 5 angular-routing angular-module angular angular-compiler

我不明白router-outletmodule's exports array编译策略之间的区别.

在此输入图像描述

为什么我们需要将它添加到exports数组中,Angular不能自动生成模块中定义的组件router-outlet.


我知道如果我想使用其他模块的组件,则需要添加到导出.

实例

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
import { M1Module } from './m1/m1.module';
// import { AppRoutingModule } from './app-routing.module';

@NgModule({
  imports: [
    BrowserModule,
    // AppRoutingModule,
    M1Module
  ],
  declarations: [AppComponent, HelloComponent],
  bootstrap: [AppComponent]
})
export class AppModule { }


----------------------

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [
    Comp1Component,
    Comp2Component
  ],
  exports: [
    Comp1Component
  ]
})
export class M1Module {}
Run Code Online (Sandbox Code Playgroud)
<hello name="{{ name }}"></hello>
<p>
  Start editing to see some magic happen :)
</p>


<app-comp1></app-comp1>
Run Code Online (Sandbox Code Playgroud)


如果我通过路由访问组件(http://example.domain.com/comp1),它不需要导出,它可以工作.

带路由的实例

import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
import { M1Module } from './m1/m1.module';
import { AppRoutingModule } from './app-routing.module';

@NgModule({
  imports: [
    BrowserModule,
    AppRoutingModule,
    M1Module
  ],
  declarations: [AppComponent, HelloComponent],
  bootstrap: [AppComponent]
})
export class AppModule { }

/*****************************************************/

import { NgModule }             from '@angular/core';
import { RouterModule, Routes } from '@angular/router';

import { Comp1Component }   from './m1/comp1/comp1.component';
import { Comp2Component }   from './m1/comp2/comp2.component';


const routes: Routes = [
  { path: 'comp1', component: Comp1Component },
  { path: 'comp2', component: Comp2Component }
];

@NgModule({
  imports: [ RouterModule.forRoot(routes) ],
  exports: [ RouterModule ]
})
export class AppRoutingModule {}


/*****************************************************/

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Comp1Component } from './comp1/comp1.component';
import { Comp2Component } from './comp2/comp2.component';

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [Comp1Component, Comp2Component],
})
export class M1Module { }
Run Code Online (Sandbox Code Playgroud)
<hello name="{{ name }}"></hello>
<p>
  Start editing to see some magic happen :)
</p>

<!-- it's need to use export -->
<!-- <app-comp1></app-comp1> -->

<!-- it doesn't need to use export -->
<router-outlet></router-outlet>
Run Code Online (Sandbox Code Playgroud)


谢谢,每个人的回答,这是我理解的总结:

按模块的exports数组加载组件

在此输入图像描述

通过路由器加载组件

在此输入图像描述

Con*_*Fan 5

正如Angular NgModule 常见问题解答中所述

我应该导出什么?

导出其他 NgModules 中的组件能够在其模板中引用的可声明类。这些是你的公开课。如果你不导出一个可声明的类,它会保持私有,只对在这个 NgModule 中声明的其他组件可见。

...

我不应该导出什么?

不要导出以下内容:

仅由路由器或引导程序动态加载的组件。永远不能在另一个组件的模板中选择此类入口组件。虽然出口它们没有坏处,但也没有好处。

...

另请注意,路由器组件会自动添加entryComponents列表中。