I am trying to work through the Angular2 tour of heroes app, and am running into bugs on the Http section of the tutorial.
At first I was getting the error:
to solve it, try this step
ng generate service InMemoryData --module=app
with your angular cli, then replace the code listed in the tutorial, and save it.app.module.ts
, and add some code in it, so the code like this.app.module.ts
import { HttpClientModule } from '@angular/common/http';
import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';
import { InMemoryDataService } from './in-memory-data.service';
@NgModule({
declarations: [
...
],
imports: [
...
HttpClientModule,
// The HttpClientInMemoryWebApiModule module intercepts HTTP requests
// and returns simulated server responses.
// Remove it when a real server is ready to receive requests.
HttpClientInMemoryWebApiModule.forRoot(
InMemoryDataService, { dataEncapsulation: false }
)
],
providers: [],
...
private heroesUrl = 'api/heroes'; // URL to web api
in hero.service.ts
hero.service.ts
...
export class HeroService {
constructor(
private http: HttpClient,
private messageService: MessageService,
) { }
private heroesUrl = 'api/heroes'; // URL to web api
/** GET heroes from the server */
getHeroes (): Observable {
return this.http.get(this.heroesUrl)
}
getHero(id: number): Observable {
...
/** Log a HeroService message with the MessageService */
private log(message: string) {
...
ng sever
, and Enjoy!.