简体   繁体   中英

Angular 2 error when injecting Http

I am trying to port a small Angular 1 app to Angular 2 and so far I've had issues with Http that I couldn't resolve. As soon as I inject HTTP_PROVIDERS, I get the following error:

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

Here's the code for my 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]);

And here's the service where I tried to use it, although it's commented for now:

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;
    }
}

If I remove HTTP_PROVIDERS from main.ts , the error disappears and my app displays fine.But ofcourse there is no http service . Either it's a bug in Angular, or I'm not doing this right (I assume it's #2).

I am using Angular 2.0.0 beta 6

Http is a separate module (not included in default bundle), so you need to include it in your main html file:

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

You can find more information at official docs , and here's the example plunker .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM