gpt4 book ai didi

javascript - 子组件在 Angular 4 中不起作用

转载 作者:搜寻专家 更新时间:2023-10-30 21:23:35 26 4
gpt4 key购买 nike

我试图显示一个子组件,但输出是一个空白屏幕。连标题都不显示。我是 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';
}

子组件

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>

最佳答案

您需要在声明中注册您的组件:

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

不是进口的。

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

关于javascript - 子组件在 Angular 4 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44788389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com