【发布时间】:2016-03-27 20:41:53
【问题描述】:
从5 minute quick start 开始,我一直在玩 angular2 测试版,遇到了一个让我难过的问题。
这是一个简化版本,显示了我遇到的问题。首先是一个完美运行的 hello world 应用程序。
package.json
{
"name": "...",
"version": "0.0.1",
"description": "...",
"author": {
"name": "...",
"email": "..."
},
"scripts": {
"tsc": "tsc",
"tsc:w": "tsc -w",
"lite": "lite-server",
"start": "concurrent \"npm run tsc:w\" \"npm run lite\" "
},
"license": "ISC",
"dependencies": {
"angular2": "2.0.0-beta.0",
"bootstrap": "^3.3.6",
"es6-promise": "^3.0.2",
"es6-shim": "^0.33.3",
"reflect-metadata": "^0.1.2",
"rxjs": "5.0.0-beta.0",
"systemjs": "0.19.6",
"zone.js": "^0.5.10"
},
"devDependencies": {
"concurrently": "^1.0.0",
"lite-server": "^1.3.1",
"typescript": "^1.7.3"
}
}
index.html
<head>
<title>Title</title>
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
<link href="styles.css" rel="stylesheet" />
<!-- 1. Load libraries -->
<script src="node_modules/es6-shim/es6-shim.js"></script>
<script src="node_modules/angular2/bundles/angular2-polyfills.js"> </script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<!-- 2. Configure SystemJS -->
<script>
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import('app/boot')
.then(null, console.error.bind(console));
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-app></my-app>
</body>
app/boot.ts
import {bootstrap} from 'angular2/platform/browser'
import {AppComponent} from './app.component'
import {AccountService} from './accounts/account.service'
bootstrap(AppComponent);
app/app.component.ts
import {Component, View} from 'angular2/core';
import {RegisterFormComponent} from './accounts/register-form.component'
@Component({
selector: 'my-app',
})
@View({
template: 'hello world',
})
export class AppComponent {
}
我想调用我的 Web Api 服务,所以我尝试关注 the docs for Http,我更新 boot.ts 如下:
新应用/boot.ts
import {bootstrap} from 'angular2/platform/browser'
import {AppComponent} from './app.component'
import {AccountService} from './accounts/account.service'
import {HTTP_PROVIDERS} from 'angular2/http';
bootstrap(AppComponent, [HTTP_PROVIDERS]);
这就是令人窒息的地方。
Chrome 给我:
uncaught SyntaxError: Unexpected token < - http:1
uncaught SyntaxError: Unexpected token < - angular2-polyfills.js:138
evaluating http://localhost:3000/angular2/http
error loading http://localhost:3000/app/boot.js
如果我尝试在我的组件上将 HTTP_PROVIDERS 设置为 viewProvider,它也会失败。
有没有其他人幸运地让 Http 在 angular2 beta 中正确注入?
- Visual Studio 2015
- Visual Studio 的 Node.JS 和 Node.JS 工具
- 使用'npm start'编译运行
【问题讨论】:
-
@GünterZöchbauer 有点像。在这种情况下,他缺少 http 包,例如
<script src=".../http.dev.js"></script> -
类似的错误,但原因不同。我有一个工作应用程序在我引导 HTTP_PROVIDERS 时会中断。他的问题的解决方案是修复 index.html 中的 System.Config 调用,这对我来说不是问题,因为在我引导 HTTP_PROVIDERS 之前一切正常
-
@EricMartinez 你是我的英雄。昨天我看了这个很久,知道这是愚蠢的。如果您提交答案,我会接受。我想我希望 angular 会为我处理包含问题?如果 import {HTTP_PROVIDERS} 语句不自动包含 JS,它的意义何在?大声笑:)
-
@Cosmosis 为了回答你的问题,Angular2 使用未来的 es6-module 导入格式。最终,浏览器将原生支持
import语法。在此之前,需要使用诸如 system.js 之类的 polyfill。
标签: npm typescript angular beta