javascript - Child component not working in angular 4 - Stack Overflow

I'm trying to display a child ponent, but the output is a blank screen. Even title is not showing.

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

appponent.ts

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

@Component({
  selector: 'app-root',
  templateUrl: './appponent.html',
  styleUrls: ['./appponent.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 './appponent';
import { CAppComponent } from './child/childponent';

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

appponent.html

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

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

app.ponent.ts

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

@Component({
  selector: 'app-root',
  templateUrl: './app.ponent.html',
  styleUrls: ['./app.ponent.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.ponent';
import { CAppComponent } from './child/child.ponent';

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

app.ponent.html

<!--The whole content below can be removed with the new code.-->
<div style="text-align:center">
  <h1>
    Wele to {{title}}!!
  </h1>
  <app-test></app-test>
</div>
Share Improve this question edited Jun 27, 2017 at 20:15 AVJT82 73.4k25 gold badges145 silver badges170 bronze badges asked Jun 27, 2017 at 19:31 Hasan ZubairiHasan Zubairi 1,1834 gold badges25 silver badges66 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You need to register your ponent in declarations:

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

not imports.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744393354a4572017.html

相关推荐

  • javascript - Child component not working in angular 4 - Stack Overflow

    I'm trying to display a child ponent, but the output is a blank screen. Even title is not showing.

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信