7

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

在此处输入图像描述

为什么我们需要将它添加到导出数组中,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 {}
<hello name="{{ name }}"></hello>
<p>
  Start editing to see some magic happen :)
</p>


<app-comp1></app-comp1>


如果我通过路由访问组件(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 { }
<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>


谢谢大家的回答,这是我理解的总结:

通过模块的导出数组加载组件

在此处输入图像描述

通过路由器加载组件

在此处输入图像描述

4

2 回答 2

5

Angular NgModule 常见问题解答中所述:

我应该出口什么?

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

...

我不应该出口什么?

不要导出以下内容:

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

...

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

于 2018-06-23T16:14:58.507 回答
1

NgModule 和范围/可见性 混淆始于组件和服务没有相同的范围/可见性:

声明/组件在本地范围内(私有可见性),提供者/服务(通常)在全局范围内(公共可见性)。这意味着您声明的组件仅在当前模块中可用。如果您需要在外部使用它们,在其他模块中,您必须将它们导出:

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

于 2018-06-28T17:55:17.157 回答