Angular 5(英雄之旅)教程:找不到模块'@ angular-in-memory-web-api'错误

时间:2018-02-27 11:27:04

标签: angular npm import angular-cli angular5

我发现了一个类似的question,但它是针对angular2&答案中的命令似乎是angular2特定的或告诉我运行相同的cmd,所以我在这里问这个问题。

我正在关注Angular 5英雄之旅tutorial&在HTTP部分,我得到了以下错误:

  

编译失败。

     

src / app / app.module.ts(13,48):错误TS2307:找不到模块'@ angular-in-memory-web-api'。

运行cmd后,我收到了一些警告,但确认安装:

λ npm install angular-in-memory-web-api --save
npm WARN ajv-keywords@3.1.0 requires a peer of ajv@^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.1.3 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

+ angular-in-memory-web-api@0.5.3
added 1 package in 25.102s

我的package.json文件正确显示了依赖关系:

    {
  "name": "hello-world",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build --prod",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^5.2.0",
    "@angular/common": "^5.2.0",
    "@angular/compiler": "^5.2.0",
    "@angular/core": "^5.2.0",
    "@angular/forms": "^5.2.0",
    "@angular/http": "^5.2.0",
    "@angular/platform-browser": "^5.2.0",
    "@angular/platform-browser-dynamic": "^5.2.0",
    "@angular/router": "^5.2.0",
    "angular-in-memory-web-api": "^0.5.3",
    "core-js": "^2.4.1",
    "rxjs": "^5.5.6",
    "zone.js": "^0.8.19"
  },
  "devDependencies": {
    "@angular/cli": "~1.7.0",
    "@angular/compiler-cli": "^5.2.0",
    "@angular/language-service": "^5.2.0",
    "@types/jasmine": "~2.8.3",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "^4.0.1",
    "jasmine-core": "~2.8.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~2.0.0",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~4.1.0",
    "tslint": "~5.9.1",
    "typescript": "~2.5.3"
  }
}

app.module.ts文件:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms'; // <-- NgModel lives here
import { AppComponent } from './app.component';
import { HeroesComponent } from './heroes/heroes.component';
import { HeroDetailComponent } from './hero-detail/hero-detail.component';
import { HeroService } from './hero.service';
import { MessagesComponent } from './messages/messages.component';
import { MessageService } from './message.service';
import { AppRoutingModule } from './app-routing.module';
import { DashboardComponent } from './dashboard/dashboard.component';
import { HttpClientModule } from '@angular/common/http';
import { HttpClientInMemoryWebApiModule } from '@angular-in-memory-web-api';
import { InMemoryDataService } from './in-memory-data.service';


@NgModule({
  declarations: [
    AppComponent,
    HeroesComponent,
    HeroDetailComponent,
    MessagesComponent,
    DashboardComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    AppRoutingModule,
    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: [HeroService, MessageService],
  bootstrap: [AppComponent]
})
export class AppModule { }

我尝试重启VS代码&amp;重新运行上面的cmd,但它没有帮助。

3 个答案:

答案 0 :(得分:3)

导入错误。 @ 不是必需的。 替换为

import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';

答案 1 :(得分:2)

如果您正在关注Angular 5巡回教程,那么错误就是那些

angular-in-memory-web-api@0.6.0需要@ angular / common @ ^ 6.0.0-rc.0的对等,但没有安装。您必须自己安装对等依赖项

angular-in-memory-web-api@0.6.0需要@ angular / core @ ^ 6.0.0-rc.0的对等方,但没有安装。您必须自己安装对等依赖项

angular-in-memory-web-api@0.6.0需要@ angular / http @ ^ 6.0.0-rc.0的对等方,但没有安装。您必须自己安装对等依赖项

angular-in-memory-web-api@0.6.0需要rxjs@^6.0.0-beta.1

的同行

您应该在内存web api版本中修改de angular,尝试安装

npm install angular-in-memory-web-api@0.5.4 --save

答案 2 :(得分:0)

我通过添加以下代码而不是HTTP CLIENT解决了这个问题

import { InMemoryWebApiModule } from 'angular-in-memory-web-api/in-memory-web-api.module';

以及在NGModule导入中

import[ 
    InMemoryWebApiModule.forRoot(
      InMemoryDataService, { dataEncapsulation: false }
    )]

对我来说很好