Angular 注入Http时出现2错误
Angular 2 error when injecting 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;
}
}
如果我从 main.ts
中删除 HTTP_PROVIDERS
,错误就会消失并且我的应用程序显示 fine.But 当然没有 http service
。要么是 Angular 中的错误,要么是我没有做对(我假设它是 #2)。
我正在使用 Angular 2.0.0 beta 6
正如 Sasxa 所指出的,事实证明我只需要将以下内容添加到我的 index.html 文件中:
<script src="node_modules/angular2/bundles/http.dev.js"></script>
Http 是一个单独的模块(不包含在默认包中),因此您需要将其包含在主 html 文件中:
<script src="https://code.angularjs.org/2.0.0-beta.6/http.dev.js"></script>
您可以在 official docs, and here's the example plunker 找到更多信息。
我正在尝试将一个小型 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;
}
}
如果我从 main.ts
中删除 HTTP_PROVIDERS
,错误就会消失并且我的应用程序显示 fine.But 当然没有 http service
。要么是 Angular 中的错误,要么是我没有做对(我假设它是 #2)。
我正在使用 Angular 2.0.0 beta 6
正如 Sasxa 所指出的,事实证明我只需要将以下内容添加到我的 index.html 文件中:
<script src="node_modules/angular2/bundles/http.dev.js"></script>
Http 是一个单独的模块(不包含在默认包中),因此您需要将其包含在主 html 文件中:
<script src="https://code.angularjs.org/2.0.0-beta.6/http.dev.js"></script>
您可以在 official docs, and here's the example plunker 找到更多信息。