I have a problem with HTTP in Angular.
I just want to GET
a JSON
list and show it in the view.
Service class
import {Injectable} from "angular2/core";
import {Hall} from "./hall";
import {Http} from "angular2/http";
@Injectable()
export class HallService {
public http:Http;
public static PATH:string = 'app/backend/'
constructor(http:Http) {
this.http=http;
}
getHalls() {
return this.http.get(HallService.PATH + 'hall.json').map((res:Response) => res.json());
}
}
And in the HallListComponent
I call the getHalls
method from the service:
export class HallListComponent implements OnInit {
public halls:Hall[];
public _selectedId:number;
constructor(private _router:Router,
private _routeParams:RouteParams,
private _service:HallService) {
this._selectedId = +_routeParams.get('id');
}
ngOnInit() {
this._service.getHalls().subscribe((halls:Hall[])=>{
this.halls=halls;
});
}
}
However, I got an exception:
TypeError: this.http.get(...).map is not a function in [null]
hall-center.component
import {Component} from "angular2/core";
import {RouterOutlet} from "angular2/router";
import {HallService} from "./hall.service";
import {RouteConfig} from "angular2/router";
import {HallListComponent} from "./hall-list.component";
import {HallDetailComponent} from "./hall-detail.component";
@Component({
template:`
<h2>my app</h2>
<router-outlet></router-outlet>
`,
directives: [RouterOutlet],
providers: [HallService]
})
@RouteConfig([
{path: '/', name: 'HallCenter', component:HallListComponent, useAsDefault:true},
{path: '/hall-list', name: 'HallList', component:HallListComponent}
])
export class HallCenterComponent{}
app.component
import {Component} from 'angular2/core';
import {ROUTER_DIRECTIVES} from "angular2/router";
import {RouteConfig} from "angular2/router";
import {HallCenterComponent} from "./hall/hall-center.component";
@Component({
selector: 'my-app',
template: `
<h1>Examenopdracht Factory</h1>
<a [routerLink]="['HallCenter']">Hall overview</a>
<router-outlet></router-outlet>
`,
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{path: '/hall-center/...', name:'HallCenter',component:HallCenterComponent,useAsDefault:true}
])
export class AppComponent { }
tsconfig.json
{
"compilerOptions": {
"target": "ES5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules"
]
}
Global import is safe to go with.
import 'rxjs/Rx';
Angular 6 - only import 'rxjs/Rx' did the trick for me
I tried below commands and it gets fixed:
Angular version 6 "0.6.8" rxjs version 6 "^6.0.0"
this solution is for :
as we all know angular is being developed every day so there are lots of changes every day and this solution is for angular 6 and rxjs 6
first to work with http yo should import it from : after all you have to declare the HttpModule in app.module.ts
and you have to add HttpModule to Ngmodule -> imports
second to work with map you should first import pipe :
third you need the map function import from :
you have to use map inside pipe like this exemple :
that works perfectly good luck !
From rxjs 5.5 onwards, you can use the pipeable operators
What is wrong with the
import 'rxjs/add/operator/map';
When we use this approach
map
operator will be patched toobservable.prototype
and becomes a part of this object.If later on, you decide to remove
map
operator from the code that handles the observable stream but fail to remove the corresponding import statement, the code that implementsmap
remains a part of theObservable.prototype
.When the bundlers tries to eliminate the unused code (a.k.a.
tree shaking
), they may decide to keep the code of themap
operator in the Observable even though it’s not being used in the application.Solution -
Pipeable operators
Pipeable operators are pure functions and do not patch the Observable. You can import operators using the ES6 import syntax
import { map } from "rxjs/operators"
and then wrap them into a functionpipe()
that takes a variable number of parameters, i.e. chainable operators.Something like this: