Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

259
Views
ts2304 no puede encontrar el nombre 'OnInit'

He trabajado a través del tutorial de superhéroe angular. Todo funciona.

Si cierro la ventana cmd ejecutando NPM, luego vuelvo a abrir una ventana CMD y vuelvo a emitir el comando NPM START, recibo dos errores

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

Puedo resolver esto eliminando

 Implements OnInit

desde ambas clases, ejecute NPM, comience a volver a agregarlas (simplemente presione CTL Z en el editor), realice algunos cambios y guarde. La aplicación se vuelve a compilar y ya estoy funcionando.

Tengo 4 clases que implementan esta función. Los he estudiado y no puedo entender qué hace que 2 fallen...

He leído publicaciones que hacen referencia a TS2304, pero esto parece ser un mensaje genérico de Función/Variable/Símbolo no encontrado...

no se que publicar Estoy feliz de publicar cualquiera de los códigos.
¿Esto se debe a errores en los módulos de los que depende (hero.ts)?

Aquí hay una clase que está fallando de esta manera. Este es el archivo hero-list.component.ts (en varios puntos de los ejemplos de demostración/en línea, también se denomina 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 answers
Answer question

0

Tienes que importar OnInit.

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

0

El tutorial no menciona que debe agregar la importación de OnInit al archivo TypeScript app.component.ts :

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

0

Tienes que importar OnInit en la parte ts.

 import { Component, OnInit } from '@angular/core';
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!