ts2304找不到名字'OnInit'

我曾经通过Angular超级英雄教程。 这一切正常。

如果我closures运行NPM的cmd窗口,然后重新打开一个CMD窗口并重新发出NPM START命令,我得到两个错误

src/app/DashBoard.component.ts(12,44) TS2304 : Cannot find name 'OnInit'. src/app/hero-list.component.ts(16, 434) TS2304 : Cannot find name 'OnInit'. 

我可以通过删除来解决此问题

 Implements OnInit 

从这两个类,运行NPM开始重新添加它们(只需在编辑器中的CTL Z)进行一些更改,保存。 该应用程序重新编译,我closures和运行。

我有4个类来实现这个function。 我研究过它们,不知道是什么让2失败

我已阅读post,参考TS2304,但这似乎是一个通用的函数/variables/符号找不到消息…

我不知道要发布什么,我很高兴发布的任何代码。
这是由模块中的错误,这取决于(hero.ts)

这里是一个以这种方式失败的类这是hero-list.component.ts文件(在演示/在线例子中的不同点,这也被命名为Heroes.component ..)

 import { Component } from '@angular/core'; import { Router } from '@angular/router'; import { Hero } from './hero'; import { HeroService } from './hero.service'; @Component({ selector: 'hero-list', templateUrl: './hero-list.component.html' , providers: [HeroService], styleUrls: [ './hero-list.component.css'] }) export class HeroListComponent implements OnInit { heroes : Hero[]; selectedHero: Hero; constructor( private router : Router , private heroService: HeroService ) { } ngOnInit(): void { this.getHeroes(); } onSelect(hero: Hero): void { this.selectedHero = hero; } getHeroes(): void { this.heroService.getHeroes().then(heroes => this.heroes = heroes); } gotoDetail() { this.router.navigate(['/detail', this.selectedHero.id]); } add(name: string): void { name = name.trim(); if (!name) { return; } this.heroService.create(name) .then(hero => { this.heroes.push(hero); this.selectedHero = null; }); } delete(hero: Hero): void { this.heroService .delete(hero.id) .then(() => { this.heroes = this.heroes.filter(h => h !== hero); if (this.selectedHero === hero) { this.selectedHero = null; } }); } } 

你必须导入OnInit。

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

本教程未能提及您需要将OnInit的导入添加到TypeScript文件app.component.ts中

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