Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

266
Vistas
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 Respuestas
Responde la pregunta

0

You have to import OnInit.

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

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 Denunciar

0

You have to import OnInit in ts part.

import { Component, OnInit } from '@angular/core';
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda