Angular2 测试版 - 引导 HTTP_PROVIDERS - "Unexpected Token <"

Angular2 beta - bootstrapping HTTP_PROVIDERS - "Unexpected Token <"

5 minute quick start 开始,我一直在玩 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);

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 如下:

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

有没有其他人有幸让 Http 在 angular2 beta 中正确注入?

当您在使用 SystemJS 时尝试导入 HTML 中未包含的内容时,会发生此错误。像 Webpack 这样的模块打包器会为你处理所有这些。

对于您的情况,您必须添加作为单独捆绑包的 Http 捆绑包,例如

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

当您尝试使用路由器并且忘记添加路由器包时,您会看到同样的错误。

从@pkozlowski-opensource 的 repos

查看这两个配置之间的区别
  • Using SystemJS :在这种情况下,如果他想使用它们,他将不得不添加 http 包或路由器包。
  • Using Webpack :在这种情况下,Webpack 会为您捆绑 bundle.js 文件中的所有内容。

很高兴对您有所帮助。

如果您使用的是 npm,请包含一个带有对本地安装的 http 引用的脚本标记:

<script src="node_modules/angular2/bundles/http.dev.js"></script>