Angular 2 未处理的 Promise 拒绝:模板解析错误:'app-main' 不是已知元素:

Angular 2 Unhandled Promise rejection: Template parse errors: 'app-main' is not a known element:

我有两个单独的模块 app.module.tscore.module.ts 用于加载两个选择器 appapp-main.

文件夹结构如下:

|main.ts
|-App
|  |-app.module.ts
|  |-Components
|    |-app.component.ts
|
|-Core
|  |-core.module.ts
|  |-Components
|    |-core.component.ts

main.ts

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppModule } from './App/app.module';
if (process.env.ENV === 'production') {
    enableProdMode();
}
platformBrowserDynamic().bootstrapModule(AppModule);

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { CoreModule } from '../Core/core.module';
import { AppComponent } from './Components/app.component';
@NgModule({
    imports: [
        BrowserModule,
        CoreModule
    ],
    declarations: [ AppComponent ],
    bootstrap: [ AppComponent ]
})
export class AppModule {}

app.component.ts

import { Component } from '@angular/core';
@Component({
    selector: 'app',
    template: `<app-main></app-main>`
})
export class AppComponent {}

core.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CoreComponent } from './Components/core.component';
@NgModule({
    imports: [
        CommonModule
    ],
    declarations: [ CoreComponent ]
})
export class CoreModule {}

core.component.ts

import { Component } from '@angular/core';
@Component({
    selector: 'app-main',
    template: ` App Loaded From Core`
})
export class CoreComponent {}

我遇到了 'app-main' is not a known element: 的问题,如下图所示: 当我将选择器更改为 app-main 时,它可以工作,但没有任何错误和输出。只返回一个空白页。

如何使用选择器在 app.component.ts 中呈现不同模块的模板?或者在单个模板中加载来自不同模块的不同模板?

您需要在 exports 中列出导入模块时应该可用的组件:

@NgModule({
    imports: [
        CommonModule
    ],
    declarations: [ CoreComponent ],
    exports: [ CoreComponent ] // <<<< added
})
export class CoreModule {}