我有一项尝试发出HTTP请求的服务。
当我运行angular时,我找不到Angular2 / http。
错误:错误:XHR错误(未找到404)加载http://localhost:3000/angular2/http(...)
service.ts
import {Injectable} from 'angular2/core';
import {Router} from 'angular2/router';
import { Http, Response,Headers,HTTP_PROVIDERS,RequestOptions,RequestMethod} from 'angular2/http';
import { Observable } from 'rxjs/Observable';
import {CommonService} from './common.service';
@Injectable()
export class AuthenticationService {
public _productUrl = 'URL-HERE';
constructor(
private _router: Router,
private _http: Http,
private _commonService: CommonService){}
main.ts
import {bootstrap} from 'angular2/platform/browser'
import {AppComponent} from './app.component'
import {ROUTER_PROVIDERS} from 'angular2/router'
import { HTTP_PROVIDERS } from 'angular2/http';
bootstrap(AppComponent, [ROUTER_PROVIDERS,HTTP_PROVIDERS]);
app.component,TS
import {Http} from 'angular2/http';
的index.html
<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>
<script src="node_modules/angular2/bundles/router.dev.js"></script>
<script src="node_modules/angular2/bundles/http.dev.js"></script>
我在这里缺少什么?
答案 0 :(得分:0)
<强> app.module.ts 强>
import {HttpModule} from '@angular/http';
...
@NgModule({
...
imports: [
HttpModule
]
})
...
答案 1 :(得分:0)
请查看您的角度版本,如果它低于beta.17,则使用&#39; angular2 / http&#39;。通常它是&#39; @ angular / http&#39;。 尝试查看最新文档。
您还可以通过查看node_modules文件夹来查看此内容。 您可能有一张名为@angular的地图而不是angular2地图。
答案 2 :(得分:0)
您导入的方式不对。您应该使用以下内容: -
import { Http } from '@angular/http';
答案 3 :(得分:0)
您正在使用以下内容:
import { Http, Response,Headers,HTTP_PROVIDERS,RequestOptions,RequestMethod} from 'angular2/http';
停止使用&#34; angular2 / http&#34;,您应该使用&#34; @ angular / http&#34;导入模块。因为它会指向角度的最新版本,以防它发生变化(我们现在处于角度4)。以此作为未来的例子:
import { Http } from '@angular/http';
答案 4 :(得分:0)
你可以试试这个:
import { Http } from '@angular/http';
and inject into the constructor(private http:Http){}
app.module.ts
import {HttpModule} from '@angular/http';
...
@NgModule({
...
imports: [
HttpModule
]
})
...
答案 5 :(得分:0)
而不是这句话
import { Http, Response,Headers,HTTP_PROVIDERS,RequestOptions,RequestMethod} from 'angular2/http';
import { Observable } from 'rxjs/Observable';
通过以下导入语句尝试一次,然后按http。
第1步:import { HttpModule } from '@angular/http';
第2步:constructor( private http: Http ) {}
答案 6 :(得分:0)
Angular CLI We are using.Follow this u can solve problem. Perfect Working for me.
app.module.ts
-------------
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http'
import { AppComponent } from './app.component';
@NgModule({
imports: [
BrowserModule,
HttpModule
],
declarations: [
AppComponent
],
providers: [{
}],
bootstrap: [AppComponent]
})
export class AppModule { }
sample.service.ts
-----------------
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
@Injectable()
export class SampleService {
public serviceUrl="someURL";
constructor(private http:Http) {
}
doSomeService(bean:any) {
let headers = new Headers({'Content-Type': 'application/json'});
return this.http.post(
serviceUrl,
JSON.stringify(bean),
{headers: headers}
).map(res => res.json())
}
}
Try this! Working. Thank you.