Cannot find module './in-memory-data-service' in tour of heroes for Angular2

前端 未结 13 974
我在风中等你
我在风中等你 2021-02-06 21:49

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:

         


        
13条回答
  •  一整个雨季
    2021-02-06 22:29

    to solve it, try this step

    1. run ng generate service InMemoryData --module=app with your angular cli, then replace the code listed in the tutorial, and save it.
    2. go to 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: [],
       ...
    
    1. Don't forget to add 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) {
       ...
    
    1. Refresh your app by running ng sever, and Enjoy!.

提交回复
热议问题