typescript ts2304 找不到名称“OnInit”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43104387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:23:56  来源:igfitidea点击:

ts2304 cannot find name 'OnInit'

node.jsangulartypescript

提问by greg

I've worked through the Angular superhero tutorial. It all works.

我已经完成了 Angular 超级英雄教程。这一切都有效。

If i close the cmd window running NPM, then re-open a CMD window and reissue the NPM START command I get two errors

如果我关闭运行 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'.

I can resolve this by removing

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

Implements OnInit

from both these classes, run NPM start re-add them (simply CTL Z in the editor) make some change , save. The app recompiles and I am off and running.

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

I have 4 classes that implement this function. I have studied them and can not figure out what makes 2 fail...

我有 4 个类来实现这个功能。我已经研究过它们,但无法弄清楚是什么让 2 失败了......

I have read posts that reference TS2304, but this seems to be a generic Function/Variable/Symbol not found message ...

我读过引用 TS2304 的帖子,但这似乎是一个通用的函数/变量/符号未找到消息......

I don't know what to post. I'm happy to post any of the code.
Is this caused by errors in modules this depends on (hero.ts)?

我不知道该发什么。我很高兴发布任何代码。
这是由依赖于 (hero.ts) 的模块中的错误引起的吗?

Here is one class that is failing in this manner. This is the hero-list.component.ts file (at various points in the demo/online examples, this is also named Heroes.component..)

这是一个以这种方式失败的课程。这是 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; }
            });
    }
}

回答by Eduardo Dennis

You have to import OnInit.

您必须导入 OnInit。

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

回答by J Woodchuck

The tutorialfails to mention you need to add the import of OnInitto TypeScript file app.component.ts:

教程没有提到您需要将OnInit的导入添加到 TypeScript 文件app.component.ts

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

回答by Akitha_MJ

Just import OnInit

只需导入 OnInit

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