我很难让我的本地代码在angular 2教程link的http部分工作。在对hero.service.ts进行更改之后,它在包含内存中的DB服务后立即停止工作。实际发生的情况是,应用程序只是拒绝加载,并卡在加载屏幕上。我已经找到了关于这个问题的各种帖子,但都没有抓住我的问题。我仍然猜测这与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发布于 2016-10-27 00:41:04
我对这个内存中的web-api有很多问题,如果不是因为这个教程,我宁愿完全避免它。我认为弹出一个简单的后端并进行真正的测试要比处理它容易得多。
我的问题是通过引用一个确切的库版本来解决的,我恐怕我不记得确切的版本是哪个了,但你可以尝试0.0.10,就像这个post中的一样。您可以在package.json中引用您需要的版本(请务必引用确切的版本,即不带"~")。
如果这不起作用,我可以尝试找出哪个版本对我有效。您还可以在控制台中检查错误,因为您可能还会遇到一些其他问题。
发布于 2016-10-27 00:41:42
这是因为"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’}
您必须像这样导入内存中的模块(通常是在AppModule中):
从‘angular-in-memory-web-api’导入{ InMemoryWebApiModule };
https://stackoverflow.com/questions/40267553
复制相似问题