我正在尝试使用 angular2 http 模块。
这是一个发出 GET 请求的角度组件:
import {Component} from '@angular/core';
import {Http} from '@angular/http';
@Component({
selector: 'fountain-app',
providers: [Http],
template: require('./main.jade')
})
export class MainComponent {
public constructor(
private $http: Http
) {
//
}
public getImage(): void {
this.$http.get('https://www.repstatic.it/content/nazionale/img/2015/11/26/121631321-63ff8328-4925-4ee2-9ff5-a81974b7074e.jpg')
.map(res => res.json()).subscribe(image => this.image = image);
}
}
这是主模块声明(注意 httpModule 导入):
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {UIView, UIRouterModule} from 'ui-router-ng2';
import {HttpModule} from '@angular/http';
import {MainComponent} from './main';
import {HeaderComponent} from './header';
import {TitleComponent} from './title';
import {FooterComponent} from './footer';
@NgModule({
imports: [
BrowserModule,
UIRouterModule.forRoot({states: STATES, configClass: MyUIRouterConfig}),
HttpModule
],
declarations: [
MainComponent,
HeaderComponent,
TitleComponent,
FooterComponent
],
bootstrap: [UIView]
})
export class AppModule {}
出于某种原因,当我开始调用时,我收到此错误:异常:./MainComponent 类 MainComponent 中的错误 - 内联模板:0:93 导致:backend.createConnection 不是函数
我不知道为什么我会得到这个
PS我知道Angular2被用来使用HTTP_PROVIDER,然后为了推送HttpModule而弃用它。
不要Http
在您的提供者数组中添加@Component
:
@Component({
selector: 'fountain-app',
template: require('./main.jade')
})
Http
它已经由 提供HttpModule
。
实际上Http
有很多依赖项,因此如果您想提供自己的 Http 服务,则必须提供这些依赖项。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句