html - Angular 4 组件呈现为空

标签 html angular

我是 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/

相关文章:

javascript - 点击功能的问题

javascript - 带有 oidc-client 的 signinSilentCallback 获得 "user not found in storage"

angular - 如何根据 Kendo Grid Angular 4 中的条件为一列分配 2 个字段

javascript - 即使评估为 True,带有复选框的 TakeUntil 也会阻止流

angular - Ag-Grid:无法减少详细信息行的容器空间

javascript - 试图从容器 div 访问特定的子 div

html - <link rel=preload> 必须有一个有效的 `as` 值

jquery - 单击 anchor 和轮播控件将关闭模式

php - 为什么Wordpress wp_update_post函数删除html表单标签?

angular - 如何在 Angular 2/4 Typescript 中顺序运行函数