简体   繁体   中英

Child component not working in angular 4

I'm trying to display a child component, but the output is a blank screen. Even title is not showing. I am new to angular please help.

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';
}

Child Component

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>

You need to register your component in declarations :

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

not imports.

Imports is used to import other modules that export components, like CommonModule .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM