注入Http时出现Angular 2错误

时间:2016-02-13 23:11:21

标签: angular angular2-http

我正在尝试将一个小的Angular 1应用程序移植到Angular 2,到目前为止,我已经遇到了Http的问题,我无法解决。一旦我注入HTTP_PROVIDERS,我就会收到以下错误:

Uncaught SyntaxError: Unexpected token <    Evaluating
http://localhost:3000/angular2/http     Error loading
http://localhost:3000/app/main.js

这是我的main.ts代码:

import {bootstrap} from 'angular2/platform/browser';
import {AppConverter} from './app.converter';
import {CurrencyService} from './services/service.currencies';
import {RippleService} from './services/service.ripple';
import {HTTP_PROVIDERS, Http} from 'angular2/http';

bootstrap(AppConverter, [CurrencyService, HTTP_PROVIDERS, RippleService]);

这是我试图使用它的服务,虽然它现在已经评论过了:

import {Http} from 'angular2/http';
import {Inject, Injectable} from 'angular2/core';
@Injectable()
export class RippleService {
    requestUrl: string= null;
    constructor(url: string) // public httpService:Http, url: string){ 
        this.requestUrl = url;
    }
}

如果我从HTTP_PROVIDERS删除main.ts,则错误消失,我的应用显示正常。但是没有http service。要么它是Angular中的一个错误,要么我没有做到这一点(我认为它是#2)。

我正在使用Angular 2.0.0 beta 6

2 个答案:

答案 0 :(得分:1)

Http是一个单独的模块(不包含在默认包中),因此您需要将其包含在主html文件中:

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

您可以在official docs找到更多信息,这里是example plunker

答案 1 :(得分:-2)

正如Sasxa指出的那样,事实证明我只需要将以下内容添加到我的index.html文件中:

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