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

7

我不理解router-outlet模块导出数组编译策略之间的区别。

enter image description here

为什么我们需要将它添加到exports数组中?Angular无法像router-outlet那样自动生成在模块中定义的组件。
我知道如果想要使用其他模块的组件,就需要将其添加到exports中。

实时样例

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>


感谢大家的回答,总结一下我理解的内容:
通过模块的导出数组加载组件。

enter image description here

通过路由加载组件

enter image description here

2个回答

5

Angular NgModule FAQs中所解释:

我应该导出什么?

导出可声明类,以便其他NgModule中的组件能在其模板中引用。这些是您的公共类。如果不导出可声明类,则它将保持私有状态,仅对在此NgModule中声明的其他组件可见。

...

我不应该导出什么?

不要导出以下内容:

只由路由器或引导动态加载的组件。这些入口组件永远无法在另一个组件的模板中选择。虽然导出它们也没有坏处,但也没有好处。

...

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


谢谢,我添加了两个图表来描述我的理解,请告诉我是否有误。 - Chunbin Li

1

NgModule和作用域/可见性 混淆的开始是组件和服务没有相同的作用域/可见性:

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

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


1
谢谢,我添加了两个图表来描述我的理解,请告诉我是否有误。 - Chunbin Li

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接