我请你谅解,在问...之前我做了很多尝试。
Angular CLI:9.0.4 节点:12.16.1 操作系统:win32 x64
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs';
import { User } from './user';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
export class UserService{
constructor(private _httpService: Http){}
getAllUsers(): Observable<User[]>{
return this._httpService.get("http://localhost:7777/webapp/user")
.map((response: Response) => response.json())
.catch(this.handleError);
}
private handleError(error: Response){
return Observable.throw(error);
}
}
“可观察”类型上不存在属性“地图”。ts(2339)
答案 0 :(得分:1)
尝试以这种方式导入rxjs映射
从'rxjs / operators'导入{map} 尽管 导入'rxjs / add / operator / map';
import { map } from 'rxjs/operators'
getUser(){
this._httpService.get(url)
.pipe(map(r => { console.log(r); return r.json()}))
.subscribe(resp => {
console.log(resp);
});
}
答案 1 :(得分:1)
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs';
import { User } from './user';
import { map } from 'rxjs/operators'
@Injectable()
export class UserService{
constructor(private _httpService: Http){}
getUsers(){
return this._httpService.get("http://localhost:7777/webapp/user")
.pipe(map((response: Response) => response.json())
.subscribe((response: Response) => {
console.log(response)
}));
}
private handleError(error: Response){
return Observable.throw(error);
}
}
类型“ OperatorFunction”上不存在属性“订阅”。ts(2339)