Angular 2 Unhandled Promise rejection:模板解析错误:'app-main'不是已知元素:

时间:2016-11-18 08:09:22

标签: angular angular2-template typescript2.0 angular2-modules

我有两个单独的模块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 is not a known element 当我将选择器更改为app-main时,它可以工作,但没有任何错误和输出。只返回一个空白页面。

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

1 个答案:

答案 0 :(得分:5)

您需要列出导入模块中应该可用的select2()中的组件:

exports