子组件在角度 4 中不起作用

Has*_*iri 2 javascript typescript angular

我正在尝试显示一个子组件,但输出是一个空白屏幕。连标题都不显示。我是 angular 的新手,请帮忙。

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';
}
Run Code Online (Sandbox Code Playgroud)

子组件

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

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

})
export class CAppComponent {

}
Run Code Online (Sandbox Code Playgroud)

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 { }
Run Code Online (Sandbox Code Playgroud)

应用程序组件.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>
Run Code Online (Sandbox Code Playgroud)

Max*_*kyi 5

您需要在以下位置注册您的组件declarations

@NgModule({
  declarations: [
    AppComponent, CAppComponent  <--------------
  ],
  imports: [
    BrowserModule, 
Run Code Online (Sandbox Code Playgroud)

不是进口。

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