【问题标题】:Angular2 tutorial app does not load (http section)Angular2 教程应用程序未加载(http 部分)
【发布时间】:2016-10-26 16:27:34
【问题描述】:

我很难让我的本地代码在 angular 2 教程 link 的 http 部分工作。在对 hero.service.ts 进行更改后,它在包含内存数据库服务后立即停止工作。实际发生的是该应用程序只是拒绝加载并卡在加载屏幕中。我发现了各种关于此的帖子,但没有一个发现我的问题。我仍然猜想这与 angular-in-memory-web-api 等有关。我不知道在哪里可以找到更多详细信息或如何解决此问题。

systemjs.config.js

/**
 * 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',
      'angular-in-memory-web-api': 'npm:angular-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'
      },
      'angular-in-memory-web-api': {
        main: './index.js',
        defaultExtension: 'js'
      }
    }
  });
})(this);

package.json

{
  "name": "angular-quickstart",
  "version": "1.0.0",
  "scripts": {
    "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
    "lite": "lite-server",
    "postinstall": "typings install",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings"
  },
  "licenses": [
    {
      "type": "MIT",
      "url": "https://github.com/angular/angular.io/blob/master/LICENSE"
    }
  ],
  "dependencies": {
    "@angular/common": "~2.1.0",
    "@angular/compiler": "~2.1.0",
    "@angular/core": "~2.1.0",
    "@angular/forms": "~2.1.0",
    "@angular/http": "~2.1.0",
    "@angular/platform-browser": "~2.1.0",
    "@angular/platform-browser-dynamic": "~2.1.0",
    "@angular/router": "~3.1.0",
    "@angular/upgrade": "~2.1.0",
    "angular-in-memory-web-api": "~0.1.5",
    "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": {
    "concurrently": "^3.0.0",
    "lite-server": "^2.2.2",
    "typescript": "^2.0.3",
    "typings":"^1.4.0"
  }
}

in-memory-data.service.ts

import { InMemoryDbService } from 'angular-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};
  }
}

浏览器控制台显示:

08:16:31.929 Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:3000/traceur
patchProperty/desc.set/wrapFn@http://localhost:3000/node_modules/zone.js/dist/zone.js:698:26
ZoneDelegate.prototype.invokeTask@http://localhost:3000/node_modules/zone.js/dist/zone.js:265:21
Zone.prototype.runTask@http://localhost:3000/node_modules/zone.js/dist/zone.js:154:28
ZoneTask/this.invoke@http://localhost:3000/node_modules/zone.js/dist/zone.js:335:28

Error loading http://localhost:3000/traceur
Unable to load transpiler to transpile http://localhost:3000/node_modules/angular-in-memory-web-api/index.js
Error loading http://localhost:3000/node_modules/angular-in-memory-web-api/index.js as "angular-in-memory-web-api" from http://localhost:3000/app/app.module.js
Stack-Trace:
(SystemJS) XHR error (404 Not Found) loading http://localhost:3000/traceur
    patchProperty/desc.set/wrapFn@http://localhost:3000/node_modules/zone.js/dist/zone.js:698:26
    ZoneDelegate.prototype.invokeTask@http://localhost:3000/node_modules/zone.js/dist/zone.js:265:21
    Zone.prototype.runTask@http://localhost:3000/node_modules/zone.js/dist/zone.js:154:28
    ZoneTask/this.invoke@http://localhost:3000/node_modules/zone.js/dist/zone.js:335:28

Error loading http://localhost:3000/traceur
Unable to load transpiler to transpile http://localhost:3000/node_modules/angular-in-memory-web-api/index.js
Error loading http://localhost:3000/node_modules/angular-in-memory-web-api/index.js as "angular-in-memory-web-api" from http://localhost:3000/app/app.module.js1localhost:3000:16:49
<anonym>localhost:3000:16
ZoneDelegate.prototype.invoke()zone.js:232
Zone.prototype.run()zone.js:114
scheduleResolveOrReject/<()zone.js:502
ZoneDelegate.prototype.invokeTask()zone.js:265
Zone.prototype.runTask()zone.js:154
drainMicroTaskQueue()zone.js:401
ZoneTask/this.invoke()zone.js:339

【问题讨论】:

  • 你在NgModule中添加了HttpModule吗?
  • 你能看到任何控制台错误并发布吗?

标签: angular


【解决方案1】:

我在使用这个 in-memory-web-api 时遇到了很多麻烦,如果不是为了教程,我宁愿完全避免它。我认为弹出一个简单的后端并进行真实测试比处理它更容易。

通过引用库的一个确切版本解决了我遇到的问题,恐怕我不记得是哪个确切版本,但您可以尝试 0.0.10,如post 中所示。您可以在 package.json 中引用您需要的版本(请务必引用准确的版本,即不带“~”)。

如果这没有帮助,我可以稍后尝试找出适合我的版本。您也可以在控制台中检查错误,因为您可能还会遇到其他问题。

【讨论】:

    【解决方案2】:

    这是因为"angular-in-memory-web-api": "~0.1.5"版本中的~抓取了最新的补丁版本,目前为0.1.13。自 0.1.5 以来发生了重大变化,涉及我们如何加载模块。

    见CHANGELOG

    转到umd 会更改您的systemjs.config 以及您导入库的方式。

    在systemjs.config.js 中,您应该将映射更改为:

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

    然后从包中删除:

    'angular-in-memory-web-api': {        
       main: './index.js',     
       defaultExtension: 'js'      
    }
    

    您必须像这样 ES 导入 in-mem 模块(通常在 AppModule 中):

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

    【讨论】:

    • 这对我不起作用。应用自述文件中提到的更改后,我在 npm start 后收到以下输出:$ npm start &gt; angular-quickstart@1.0.0 start /home/maik/repos &gt; tsc &amp;&amp; concurrently "tsc -w" "lite-server" app/main.ts(1,1): error TS6053: File '/home/maik/repos/app/node_modules/angular2/typings/browser.d.ts' not found. node_modules/@types/core-js/index.d.ts(21,14): error TS2300: Duplicate identifier 'PropertyKey'. node_modules/@types/core-js/index.d.ts(1513,5): error TS2300: Duplicate identifier 'export='...and so on.
    猜你喜欢
    • 2018-07-15
    • 2016-12-13
    • 2016-09-21
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 2016-12-30
    相关资源
    最近更新 更多