子组件不工作角4

时间:2017-06-27 19:31:02

标签: javascript angular typescript

我试图显示子组件,但输出是空白屏幕。甚至标题也没有显示。我是新来的角,请帮助。

app.component.ts

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
}

子组件

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

@Component({
  selector: 'app-test',
  template: '<h1>Haa</h1>',

})
export class CAppComponent {

}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { CAppComponent } from './child/child.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule, CAppComponent
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.html

<!--The whole content below can be removed with the new code.-->
<div style="text-align:center">
  <h1>
    Welcome to {{title}}!!
  </h1>
  <app-test></app-test>
</div>

1 个答案:

答案 0 :(得分:4)

您需要在declarations注册您的组件:

@NgModule({
  declarations: [
    AppComponent, CAppComponent  <--------------
  ],
  imports: [
    BrowserModule, 

不是进口。

Imports用于导入导出组件的其他模块,如CommonModule