如何显示新创建的组件

时间:2019-04-10 15:54:50

标签: html angular components

我正在将Angular CLI 7.3.8与Node 10.12.0一起使用。我首先使用ng new tpangular创建一个新的“应用程序”,然后移动到该目录并使用ng serve --open在计算机上启动该应用程序。然后,我停止执行以使用ng generate component etudiants创建新组件。到目前为止,一切都已创建,现在我想在index.html文件中切换出app-etudiants的app-root,期望在etudiants.component.html中看到基本文本,但什么也没发生。没有文字显示。

我尝试清理etudiants.component.ts文件,使其与app.component.ts相匹配,期望复制粘贴方法至少可以执行某些操作,但是不行,没有任何效果。我见过有人说您需要向我尝试过的组件添加指令属性,但出现错误'directives' does not exist in type 'Component'

这是index.html文件:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Tpangular</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-etudiants></app-etudiants>
</body>
</html>

etudiants.component.html:

<p>
  etudiants works!
</p>

和etudiants.component.ts:

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

@Component({
  selector: 'app-etudiants',
  templateUrl: './etudiants.component.html',
  styleUrls: ['./etudiants.component.css'],
})
export class EtudiantsComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

我还没有找到任何解决方案,我真的很想了解发生了什么,因为我在执行过程中看不到任何错误。

1 个答案:

答案 0 :(得分:2)

您无需更改应用程序根目录,而必须将<app-etudiants></app-etudiants>添加到文件app.component.html中。试试吧。

相关问题