Angular2 observable share is not working
Angular2 Observable 共享不工作,重复的 http 调用正在进行
BuildingService.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | @Injectable() export class BuildingService { constructor(private http: Http){ } buildings$: Observable<Building[]>; this.buildings: Building[]; getData() : Observable<Building[]>{ this.buildings$ = this.http.get('http://localhost:8080/buildings').share().map(this.extractData); this.buildings$.subscribe(buildings => this.buildings = buildings); return this.buildings$; } private extractData(res: Response) { let body = res.json(); return body; } } |
component1.ts
1 2 3 4 5 6 7 | export class component1 { constructor( private buildingService: BuildingService) {} this.subscription = this.buildingService.getData() .subscribe(buildings => console.log(buildings), error => this.errorMessage = error); } |
component2.ts
1 2 3 4 5 6 7 | export class component2 { constructor( private buildingService: BuildingService) {} this.subscription = this.buildingService.getData() .subscribe(buildings => console.log(buildings), error => this.errorMessage = error); } |
share 不工作,多个 http 调用正在进行。即使我尝试了此链接中的代码
但没用。
有人可以告诉我如何避免使用 Angular Observable 进行重复的 http 调用吗?
我认为这只是对
当你调用
如果您在 HTTP 请求挂起时进行另一个订阅,它只会将另一个观察者添加到
你想要的是
您正在使用此处的每个
1 | this.buildings$ = this.http.get('http://localhost:8080/buildings').share().map(this.extractData); |
如果你想在组件之间"共享"数据并防止多次调用,你很可能必须使用 rxjs 的重播功能,例如,你可以这样做:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | @Injectable() export class BuildingService { constructor(private http: Http){} buildings$: Observable<Building[]>; this.buildings: Building[]; getData(fetchNew: boolean = false) : Observable<Building[]>{ if (fetchNew || !this.buildings$) { this.buildings$ = this.http.get('http://localhost:8080/buildings') .map(this.extractData) .publishReplay(1) .refCount(); this.buildings$.subscribe(buildings => this.buildings = buildings); } return this.buildings$; } private extractData(res: Response) { let body = res.json(); return body; } } |
您在服务和组件中都订阅了 observable。试试这个作为你服务中的 getData 方法:
1 2 3 4 | getData() : Observable<Building[]>{ return this.http.get('http://localhost:8080/buildings') .map(this.extractData); } |