Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
ts2304 cannot find name 'OnInit'

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

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

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.

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

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

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)?

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..)

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; }
            });
    }
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You have to import OnInit.

import { Component, OnInit } from '@angular/core';
over 4 years ago · Santiago Trujillo Relatório

0

The tutorial fails to mention that you need to add the import of OnInit to TypeScript file app.component.ts:

import { Component, OnInit } from '@angular/core';
over 4 years ago · Santiago Trujillo Relatório

0

You have to import OnInit in ts part.

import { Component, OnInit } from '@angular/core';
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda