Angular 4中子组件无法工作

3

我是一名新手,正在尝试显示一个子组件,但输出的屏幕是空白的,甚至标题也没有显示。请帮忙解决。

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个回答

6
你需要在declarations中注册你的组件:
@NgModule({
  declarations: [
    AppComponent, CAppComponent  <--------------
  ],
  imports: [
    BrowserModule, 

不是导入。

导入用于导入其他模块,这些模块导出组件,例如 CommonModule


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接