调用函数'InMemoryWebApiModule'时,angular2不支持函数调用

时间:2017-02-16 03:31:18

标签: angular

我正在用cli制作angular2教程。

当我提供HTTP服务时(教程步骤7),我发现了一个问题。

尝试发球后:

Calling function 'InMomoryWebApiModule', function calls are not supported.
webpack: Failed to compile.

main.ts:

import { platformBrowserDynamic }           from '@angular/platform-browser-dynamic';
import { enableProdMode }                   from '@angular/core';
import { environment }                      from './environments/environment';
import { AppModule }                        from './app/app.module';
import{InMemoryBackendService, SEED_DATA }  from 'angular2-in-memory-web-api'

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule);

app.module.ts:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule }   from '@angular/forms';
import { HttpModule }    from '@angular/http';

import { AppRoutingModule }     from './app-routing.module';

// Imports for loading & configuring the in-memory web api
import { InMemoryWebApiModule } from 'angular2-in-memory-web-api';
import { InMemoryDataService }  from './in-memory-data.service';

import { AppComponent }        from './app.component';
import { DashboardComponent }  from './dashboard.component';
import { HeroDetailComponent } from './hero-detail.component';
import { HeroesComponent }     from './heroes.component';
import { HeroService }         from './hero.service';
import { HeroSearchComponent } from './hero-search.component';

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    InMemoryWebApiModule.forRoot(InMemoryDataService),
    AppRoutingModule
  ],
  declarations: [
    AppComponent,
    DashboardComponent,
    HeroDetailComponent,
    HeroesComponent,
    HeroSearchComponent
  ],
  providers: [HeroService],
  bootstrap: [AppComponent]
})

export class AppModule { }

内存-data.service.ts:

import { InMemoryDbService } from 'angular2-in-memory-web-api';
export class InMemoryDataService implements InMemoryDbService {
  createDb() {
    let heroes = [
      {id: 11, name: 'Mr. Nice'},
      {id: 12, name: 'Narco'},
      {id: 13, name: 'Bombasto'},
      {id: 14, name: 'Celeritas'},
      {id: 15, name: 'Magneta'},
      {id: 16, name: 'RubberMan'},
      {id: 17, name: 'Dynama'},
      {id: 18, name: 'Dr IQ'},
      {id: 19, name: 'Magma'},
      {id: 20, name: 'Tornado'}
    ];
    return {heroes};
  }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

您似乎使用旧版https://github.com/angular/in-memory-web-api

安装npm install --save angular-in-memory-web-api代替(没有2),该版本应该没问题。旧版本无法进行静态分析,导致AoT出现问题。