Я не понимаю разницы между стратегиями компиляции router-outlet
и module's exports array
.
router-outlet
автоматически сгенерирует компонент с помощью ComponentFactoryResolver< /а>- если вы не используете
router-outlet
для доступа к компоненту другого модуля, он выдаст ошибку из парсер шаблонов
Почему нам нужно добавить его в массив экспорта, 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>
если я обращаюсь к компоненту через routing(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>
спасибо, все ответили, это резюме, которое я понимаю:
загрузить компонент по массиву экспорта модуля
загрузить компонент по маршрутизатору