for - Angular 2: Ningún proveedor para ConnectionBackend
error: staticinjectorerror(appmodule)[http-> connectionbackend]: (1)
Obtenga este "No hay proveedor para ConnectionBackend!" error al tratar de usar http
con una promesa.
main.ts
// ... tl;dr import a bunch of stuff
platformBrowserDynamic().bootstrapModule(MyModule);
myModule.ts
// ... tl;dr import a bunch of stuff
@NgModule({
declarations: [
PostComponent
],
providers: [
Actions,
MyService,
Http
],
imports: [
...
],
bootstrap: [MyComponent]
})
myComponent.ts
import { Component, OnInit } from ''@angular/core'';
import {ActivatedRoute, Router} from ''@angular/router'';
import {Observable} from ''rxjs/Observable'';
import {MyService} from ''./../services/myService''
import {Post} from ''./post'';
@Component({
templateUrl: ''post.component.html'',
styleUrls: [''post.component.css'']
})
export class MyComponent implements OnInit {
post: Observable<Post>;
postId : Number;
constructor(private route: ActivatedRoute, private router: Router, private service:MyService) {
}
ngOnInit() {
this.route.params.subscribe(params => {
this.postId = +params[''id''];
this.post = this.service.getPostById(this.postId);
});
}
}
myService.ts
import {Injectable} from "@angular/core"
import {Http} from ''@angular/http'';
import {Observable} from ''rxjs/Observable'';
import {Post} from ''./../post/post'';
import ''rxjs/Rx''
@Injectable()
export class MyService {
endpoint_url:String = "http://someurl.com/";
constructor(private http: Http){
}
getPostById (id:Number) : Observable<Post> {
return this.http.get(this.endpoint_url + id.toString()).map(res => res.json());
}
}
Todo se ve bien pero luego me sale este error:
error_handler.js:51Error: Uncaught (in promise): Error: Error in ./MyComponent class MyComponent_Host - inline template:0:0 caused by: No provider for ConnectionBackend!
at resolvePromise (zone.js:429)
at zone.js:406
...
Y aquí están los documentos para ConnectionBackend . ¿Creo que solo necesito agregar algo a los providers
en myModule
?
Importe el HttpModule en su módulo en lugar de registrar Http
como proveedor.
import {HttpModule} from ''@angular/http'';
@NgModule({
imports: [HttpModule],
declarations: [
PostComponent
],
providers: [
Actions,
MyService
],
bootstrap: [MyComponent]
})
El HttpModule registra proveedores para todos sus servicios.