导入要素模块时未加载Angular 2主要组件,但加载未导出的要素模块中的组件

时间:2017-02-26 14:17:41

标签: angular

我有一个功能模块noi-dung.module.ts

import { NgModule } from "@angular/core";
import { CommonModule } from "@angular/common";

import { NoiDungListComponent } from "./noi-dung-list/noi-dung-list.component";
import { NoiDungService } from "./shared/noidung.service";
import { NoiDungRoutingModule } from "./noi-dung.routing";

@NgModule({
    imports: [CommonModule, NoiDungRoutingModule],
    declarations: [NoiDungListComponent],
    providers: [NoiDungService]
})
export class NoiDungModule {
}

我将它导入我的AppModule

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app.routing';


//feature module
import { NoiDungModule } from './noi-dung/noi-dung.module';

@NgModule({
  imports: [
    BrowserModule,
    HttpModule,
    NoiDungModule,
    FormsModule,
    AppRoutingModule,
  ],
  declarations: [
    AppComponent,
  ],
  providers: [
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

我收到此错误

"Template parse errors:
'my-app' is not a known element:
1. If 'my-app' is an Angular component, then verify that it is part of this module.
2. If 'my-app' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("

<body>
    [ERROR ->]<my-app>Loading...</my-app>
</body>

"): NoiDungListComponent@27:4"

它告诉我my-app选择器不在NoiDungListComponent中 但我不是导出noi-dung.module.ts

中的那个组件

my-app选择器位于AppComponent

import {Component} from '@angular/core';

@Component({
    selector:'my-app',
    templateUrl: './app/app.html'
})
export class AppComponent {
    title = 'App Angular 2 Base';
}

AppComponent是一个引导程序并在主模块中声明

那么为什么它加载了NoiDungComponent里面的noi-dung module并且没有导出到外部而不是声明和引导的组件?

[编辑]在评论中提出答案

1 个答案:

答案 0 :(得分:1)

<body>
  <my-app>Loading...</my-app>
</body>

应仅在index.html中,而不在组件模板中。