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

260
Views
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 answers
Answer question

0

You have to import OnInit.

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

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 Report

0

You have to import OnInit in ts part.

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!