I am a beginner in typescript and I have been following a tutorial called Tour of Heroes on Angular's website. In the final chapter of the tutorial, when I tried to use HTTP with the provided code, everything seemed to run fine but I encountered an error from the typescript compiler.
https://i.sstatic.net/iqye0.png
The issue is that because of this error, I have to refresh the browser as BrowserSync is not syncing properly due to the error. Can anyone help me with this?
Below is the code:
Hero.service.ts
import { HEROES }from './mock.heroes';
import { Hero } from './hero';
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class HeroService {
url: string;
constructor(private http: Http){
this.url = "http://jsonplaceholder.typicode.com/users";
this.http = http;
}
getHeros(): Promise<Hero[]> {
if(!HEROES || HEROES.length == 0) {
return this.http.get(this.url)
.toPromise()
.then(response => {
HEROES = response.json();
});
} else {
let promise: Promise<Hero[]> = new Promise<Hero[]>(function(resolve, reject){
resolve(HEROES);
});
return promise;
}
}
}
mock.heroes.ts
import { Hero } from './hero';
export var HEROES: Hero[];