Angular2 tutorial HTTP 的怪問題。

在照著 Angular 2 官方的tutorial 教學練習的時候,在Chapter 7 Http 一開始設定好 in-memory-data.service.tsapp.module.ts之後,原本我們寫死的moch-heroes資料轉成透過 http 跟memory拿資料的方法會失敗!會出現下列的錯誤訊息:
GET http://localhost:3000/traceur 404 (Not Found)
Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:3000/traceur
    Error: XHR error (404 Not Found) loading http://localhost:3000/traceur
        at XMLHttpRequest.wrapFn [as _onreadystatechange] (http://localhost:3000/node_modules/zone.js/dist/zone.js:698:29)
        at ZoneDelegate.invokeTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:265:35)
        at Zone.runTask (http://localhost:3000/node_modules/zone.js/dist/zone.js:154:47)
        at XMLHttpRequest.ZoneTask.invoke (http://localhost:3000/node_modules/zone.js/dist/zone.js:335:33)





問題主要是出在 angular-in-memory-web-api 所以要修改:


  1.  systemjs.config.js 內將angular-in-memory-web-api改成 angular2-in-memory-web-api
    /**
     * System configuration for Angular samples
     * Adjust as necessary for your application needs.
     */
    (function (global) {
      System.config({
        paths: {
          // paths serve as alias
          'npm:': 'node_modules/'
        },
        // map tells the System loader where to look for things
        map: {
          // our app is within the app folder
          app: 'app',
          // angular bundles
          '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
          '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
          '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
          '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
          '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
          '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
          '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
          '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
          // other libraries
          'rxjs':                      'npm:rxjs',
          'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
          app: {
            main: './main.js',
            defaultExtension: 'js'
          },
          rxjs: {
            defaultExtension: 'js'
          },
          'angular2-in-memory-web-api': {
            main: './index.js',
            defaultExtension: 'js'
          }
        }
      });
    })(this);
    
  2. package.json 內將 angular-in-memory-web-api 改成 angular2-in-memory-web-api 版本改成0.0.21(我當時的最新版)
    {
        "name": "angular-quickstart",
        "version": "1.0.0",
        "scripts":{
            "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
            "lite": "lite-server",
            "tsc": "tsc",
            "tsc:w": "tsc -w"
        },
        "licenses": [
            {
              "type": "MIT",
              "url": "https://github.com/angular/angular.io/blob/master/LICENSE"
            }
        ],
        "dependencies": {
            "@angular/common": "~2.1.1",
            "@angular/compiler": "~2.1.1",
            "@angular/core": "~2.1.1",
            "@angular/forms": "~2.1.1",
            "@angular/http": "~2.1.1",
            "@angular/platform-browser": "~2.1.1",
            "@angular/platform-browser-dynamic": "~2.1.1",
            "@angular/router": "~3.1.1",
            "@angular/upgrade": "~2.1.1",
            "angular2-in-memory-web-api": "~0.0.21",
            "bootstrap": "^3.3.7",
            "core-js": "^2.4.1",
            "reflect-metadata": "^0.1.8",
            "rxjs": "5.0.0-beta.12",
            "systemjs": "0.19.39",
            "zone.js": "^0.6.25"
        },
        "devDependencies": {
            "@types/core-js": "^0.9.34",
            "@types/node": "^6.0.45",
            "concurrently": "^3.0.0",
            "lite-server": "^2.2.2",
            "typescript": "^2.0.3"
        }
    }
    
  3. app.module.ts 內把導入 angular-in-memory-web-api 改成:
    import { InMemoryWebApiModule } from 'angular2-in-memory-web-api';

接著下npm install 來安裝angular2-in-memory-web-api就可以正常使用npm start解決這個錯誤。

REF:

留言