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

时间:2018-06-17 09:02:11

标签: angular angular-routing angular-module angular-compiler

我不理解router-outletmodule's exports array编译策略之间的区别。

enter image description here

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

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

live example

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 {}
<hello name="{{ name }}"></hello>
<p>
  Start editing to see some magic happen :)
</p>


<app-comp1></app-comp1>

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

live example with routing

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

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

按模块的导出数组加载组件

enter image description here

按路由器加载组件

enter image description here

2 个答案:

答案 0 :(得分:4)

Angular NgModule FAQs中所述:

  

我应该导出什么?

     

导出组成组件的可声明类   其他NgModule可以在其模板中引用。这些是   您的公开课。如果您不导出可声明的类,它将保留   私有的,仅对此NgModule中声明的其他组件可见。

     

...

  

我不应该导出什么?

     

不导出以下内容:

     

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

     

...

还要注意,路由器组件在entryComponents列表中automatically added

答案 1 :(得分:1)

NgModule和范围/可见性 混乱始于具有不同范围/可见性的组件和服务:

声明/组件在本地范围内(不公开), 提供者/服务(通常)在全球范围内(公众可见度)。 这意味着您声明的组件仅在当前模块中可用。如果需要在外部使用它们,请在其他模块中导出它们:

但是路由器出口会在运行时加载特定的组件,因此我们不需要导出它们。(这就是我的理解,如果我错了,请原谅我)