angular2-in-memory-web-api 404 错误 [英] angular2-in-memory-web-api 404 error

查看:20
本文介绍了angular2-in-memory-web-api 404 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过本指南以 angular 2 构建 5 分钟的应用程序:https://angular.io/docs/ts/latest/tutorial/toh-pt6.html.

I'm trying to build the 5 min app in angular 2 by this guide: https://angular.io/docs/ts/latest/tutorial/toh-pt6.html.

在 http 部分,我添加了一个假服务器,但由于 angular2-in-memory-web-api 出现 404 错误.

In the http part, I added a fake server but I get 404 error because the 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 教程(英雄之旅):找不到模块 'angular2-in-memory-web-api'

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

But he didn't use angular-cli + I don't have a clue where to add those dependencies they talked about there.

我的 main.ts:

My main.ts:

// 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:

This is my 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"
  }
}

我需要做什么才能消除该错误?

What do I need to do to make that error go away?

推荐答案

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

然后在main.ts:

Then in 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:

and add this to angular-cli-build.js in your projects root directory:

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

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

Finally restart the server and run ng build before ng serve.

这篇关于angular2-in-memory-web-api 404 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆