Angular2 beta - 启动HTTP_PROVIDERS时出现“Unexpected Token <”

24

5分钟快速入门开始,我一直在尝试使用Angular2 beta,并遇到了一个让我困惑的问题。

这里有一个简化版本来展示我遇到的问题。首先,这是一个完美运行的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);

应用程序/应用程序组件.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服务,因此我试图遵循Http文档,我将boot.ts更新如下:

新的app/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,它也会失败。

是否有其他人在使用angular2 beta中成功注入Http的经验?

  • Visual Studio 2015
  • Node.JS和Node.JS工具(适用于Visual Studio)
  • 使用“NPM start”编译和运行

这是一个重复的 https://dev59.com/-1sW5IYBdhLWcg3w-rIA 吗? - Günter Zöchbauer
3
@GünterZöchbauer 差不多。在这种情况下,他缺少了 http 捆绑包,例如 <script src=".../http.dev.js"></script> - Eric Martinez
类似的错误,但原因不同。我有一个工作正常的应用程序,在引导HTTP_PROVIDERS时会出现问题。他的问题的解决方案是在index.html中修复System.Config调用,但在我的情况下这并不是问题,因为一切都运行得很完美,直到我引导HTTP_PROVIDERS。 - Cosmosis
@EricMartinez 你是我的英雄。昨天我看了很久,知道这是一些愚蠢的东西。如果你提交一个答案,我会接受它。我想我希望angular会为我处理包含?如果它不自动包含JS,那么import {HTTP_PROVIDERS}语句的意义是什么呢?哈哈 :) - Cosmosis
@Cosmosis 回答你的问题,Angular2使用未来的es6模块导入格式。最终,浏览器将原生支持import语法。在此之前,需要使用像system.js这样的polyfill。 - Evan Plaice
2个回答

52

当使用SystemJS时,如果您尝试导入未包含在HTML中的内容,则会出现此错误。像Webpack这样的模块打包工具会为您处理这一切。

对于您的情况,您需要添加Http bundle作为单独的捆绑包,例如:

<script src="https://code.angularjs.org/2.0.0-beta.0/http.dev.js"></script>

当您尝试使用路由器但忘记添加路由器包时,您将看到此相同的错误。

请检查 @pkozlowski-opensource 存储库中这两种配置之间的区别:

  • 使用 SystemJS:对于这种情况,如果想使用它们,他将不得不添加http包或路由器包。
  • 使用 Webpack:在这种情况下,Webpack 将所有内容捆绑在那个 bundle.js 文件中。

很高兴帮到您。


1
我发现JSPM也很适合打包Angular2模块。 - brando
@brando 除非你想将它作为独立的捆绑包使用,否则 JSPM 已经提供了 angular2 的安装方式 jspm install angular2 - Evan Plaice
唉,这个错误消息应该比这个更好。 - allenhwkim

11
如果您正在使用npm,请在脚本标签中包含对本地安装的http引用:
<script src="node_modules/angular2/bundles/http.dev.js"></script>

1
这应该是截至2016年3月的被接受的答案。如果其他人也遇到了这个问题,这就是解决方法。 - Matthew Pitts

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接