angular2-in-memory-web-api 404错误

时间:2016-07-08 21:03:46

标签: javascript angular npm angular-cli angular2-http

我试图通过本指南在角度2中构建5分钟应用: https://angular.io/docs/ts/latest/tutorial/toh-pt6.html

在http部分,我添加了一个虚假的服务器,但我得到404错误,因为angular2-in-memory-web-api。

http://localhost:4200/vendor/angular2-in-memory-web-api/in-memory-backend.service.js
Failed to load resource: the server responded with a status of 404 (Not Found)

我试图遵循这个答案:Angular2 Tutorial (Tour of Heroes): Cannot find module 'angular2-in-memory-web-api'

但他没有使用angular-cli +我不知道在哪里添加他们在那里谈到的那些依赖。

我的主要内容:

// Imports for loading & configuring the in-memory web api
import { XHRBackend } from '@angular/http';

import { InMemoryBackendService, SEED_DATA } from 'angular2-in-memory-web-api';
import { InMemoryDataService }               from './app/in-memory-data.service';

// The usual bootstrapping imports
import { bootstrap } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { AppComponent, environment } from './app/';
import { APP_ROUTER_PROVIDERS } from './app/app.routes';
import { HTTP_PROVIDERS } from '@angular/http';
if (environment.production) {
  enableProdMode();
}

bootstrap(AppComponent, [
  APP_ROUTER_PROVIDERS,
  HTTP_PROVIDERS,
  { provide: XHRBackend, useClass: InMemoryBackendService }, // in-mem server
  { provide: SEED_DATA, useClass: InMemoryDataService }      // in-mem server data
]);

这是我的package.json:

{
  "name": "angular2-projects",
  "version": "0.0.0",
  "license": "MIT",
  "angular-cli": {},
  "scripts": {
    "start": "ng serve",
    "postinstall": "typings install",
    "lint": "tslint \"src/**/*.ts\"",
    "test": "ng test",
    "pree2e": "webdriver-manager update",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "2.0.0-rc.4",
    "@angular/compiler": "2.0.0-rc.4",
    "@angular/core": "2.0.0-rc.4",
    "@angular/forms": "0.2.0",
    "@angular/http": "2.0.0-rc.4",
    "@angular/platform-browser": "2.0.0-rc.4",
    "@angular/platform-browser-dynamic": "2.0.0-rc.4",
    "@angupackage.lar/router": "3.0.0-beta.1",
    "@angular/router-deprecated": "2.0.0-rc.2",
    "es6-shim": "0.35.1",
    "reflect-metadata": "0.1.3",
    "rxjs": "5.0.0-beta.6",
    "systemjs": "0.19.26",
    "zone.js": "0.6.12"
  },
  "devDependencies": {
    "angular-cli": "1.0.0-beta.9",
    "codelyzer": "0.0.20",
    "ember-cli-inject-live-reload": "1.4.0",
    "jasmine-core": "2.4.1",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "0.13.22",
    "karma-chrome-launcher": "0.2.3",
    "karma-jasmine": "0.3.8",
    "protractor": "3.3.0",
    "ts-node": "0.5.5",
    "tslint": "3.11.0",
    "typescript": "1.8.10",
    "typings": "0.8.1"
  }
}

如果要让错误消失,我需要做些什么?

4 个答案:

答案 0 :(得分:3)

回答一个旧帖子,但我的解决方案不同,所以我想我会在这里发布。

我得到了使用最新包并使用angular(4及以上)提到的完全相同的错误。

原因是我app.module.ts我在InMemoryWebApiModule之前导入了HttpModule。把InMemoryWebApiModule放在HttpModule之后(毫无疑问应该是那样)就可以了。

<强>错误

imports: [
      BrowserModule,
      FormsModule,
      InMemoryWebApiModule.forRoot(InMemoryDataService),
      HttpModule,
      JsonpModule,
]

<强>正确

imports: [
      BrowserModule,
      FormsModule, 
      HttpModule,
      JsonpModule,
      InMemoryWebApiModule.forRoot(InMemoryDataService) //this has to be imported after httpModule
]

答案 1 :(得分:2)

首先,您需要npm i angular2-in-memory-web-api --save

然后进去 main.ts:

import{InMemoryBackendService, SEED_DATA } from 'angular2-in-memory-web-api'

src/system-config.js中的

const barrels: string[] = [
  // Angular specific barrels.
  '@angular/core',
  '@angular/common',
  '@angular/compiler',
  '@angular/forms',
  '@angular/http',
  '@angular/router',
  '@angular/platform-browser',
  '@angular/platform-browser-dynamic',


  // Thirdparty barrels.
  'rxjs',
  'angular2-in-memory-web-api',

  // App specific barrels.
  'app',
  'app/shared',
  /** @cli-barrel */
];



    // Apply the CLI SystemJS configuration.
    System.config({
      map: {
        '@angular': 'vendor/@angular',
        'rxjs': 'vendor/rxjs',
        'main': 'main.js',
        'angular2-in-memory-web-api': 'vendor/angular2-in-memory-web-api'
      },

并将其添加到项目根目录中的angular-cli-build.js

   'angular2-in-memory-web-api/**/**/*.js'`

最后重新启动服务器并在ng build之前运行ng serve

答案 2 :(得分:2)

添加最新版本的angular2-in-memory-web-api的答案,因为我已经花了好几个小时试图找到404错误。

1)首先,对于新angular-in-memory-web-api,不推荐使用angular2-in-memory-web-api,这意味着您需要确保将package.json更改为具有最新版本:

"angular2-in-memory-web-api": "0.0.6",

需要更改为:

"angular-in-memory-web-api": "^0.1.3",

2)记得修改systemjs.config.js(if in angular page tutorial for example as my case):

'angular-in-memory-web-api': 'node_modules/angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',

3)在您的模块中,导入更改为最新版本:

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

需要更改为新声明:

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

4)然后重新运行npm install和应用程序应该运行最新版本没有任何故障。这解决了我的问题。

答案 3 :(得分:0)

您错过了内存网络API的app.module配置,这是本教程的一部分:angular-toh-simulating-the-web-api