我是 Angular 的新手,我正在尝试定义一个组件并在我的主页中使用它。
问题是,当使用 index.html 中的组件时,我只能看到我的 <custom-component></custom-component>
空的,里面什么都没有。
所以我做的是:
在 Angular cli 中:ng 生成自定义组件。
在 custom.component.html 中,段落标记内只有一个文本。
在 index.html 中,我插入了在 custom.component.ts (app-custom) 中找到的选择器作为标记。
- 在 app.module.ts 中我导入了自定义组件。
- ng serve 仅输出 app-custom 标签,没有应包含在其中的段落标签。
我错过了什么?
更新: 我的组件代码:
自定义组件.html
<p>
component works!
</p>
自定义组件.ts
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-custom',
templateUrl: './custom.component.html',
styleUrls: ['./custom.component.css']
})
export class CustomComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
index.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>TestApp</title>
<base href="/">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-custom></app-custom>
</body>
</html>
应用程序模块.ts:
import { CustomComponent } from './custom/custom.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { FlexLayoutModule } from '@angular/flex-layout';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent,
CustomComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
BrowserAnimationsModule,
FlexLayoutModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
最佳答案
尝试在声明后添加导出 => [AppComponent, CustomComponent] ?在应用程序模块中。或者使用最新版本创建一个新的 cli 项目
关于html - Angular 4 组件呈现为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47300538/