我试图找出如何在我的角度类中使用async / await。 我有一个PostService从api获取帖子,但依赖于AreaService,以便PostService知道从哪个区域获取帖子。无法弄清楚如何告诉我的角度类PostService等待来自我的AreaService的数据。 来自AreaService的正确响应将是字符串" fun"。然后,PostService将在this.areas数组中使用此有趣字符串,以便从当前所选区域向用户显示帖子 当前错误是:未捕获(在承诺中):错误:错误:0:0导致:无法读取属性' name'未定义的
postservice
import { Injectable, OnDestroy } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import { Observable} from 'rxjs';
import 'rxjs/add/operator/map';
import { AuthenticationService, AreaService, MessageService} from './index';
import { Post, Area} from '../_models/index';
@Injectable()
export class PostService {
areas: Area[] = [];
constructor(
private http: Http,
private authenticationService: AuthenticationService,
private areaService: AreaService,
private messageService: MessageService
) {
}
getPosts(): Observable<Post[]> {
this.areaService.getAreas().subscribe(area => { this.areas = area;});
// add authorization header with jwt token
let headers = new Headers();
headers.append('Authorization','token ' + this.authenticationService.token);
headers.append('Content-Type', 'application/json');
let options = new RequestOptions({
headers: headers
});
// get posts from api
return this.http.get('http://localhost:8000/areas/'+ this.areas[0].name +'/', options)
.map((response: Response) => response.json());
}
}
areaservice
import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions, Response } from '@angular/http';
import { Observable, Subject } from 'rxjs';
import 'rxjs/add/operator/map';
import { AuthenticationService, MessageService } from './index';
import { Area } from '../_models/index';
@Injectable()
export class AreaService {
public areas: Observable<Area[]> ;
subject:Subject<Area[]> = new Subject();
public currentArea: string = "fun";
constructor(
private http: Http,
private authenticationService: AuthenticationService,
private messageService: MessageService) {
}
getAreas(): Observable<Area[]> {
// add authorization header with jwt token
let headers = new Headers();
headers.append('Authorization','token ' + this.authenticationService.token);
headers.append('Content-Type', 'application/json');
let options = new RequestOptions({
headers: headers
});
// get areas from api
return this.http.get('http://localhost:8000/areas/', options)
.map((response: Response) => response.json());
// this.areas = response.json();
// console.log("h");
// this.messageService.sendMessage(this.areas[0].name);
//this.messageService.sendMessage(this.areas[0].name);
}
}
area.ts
import { Injectable } from '@angular/core';
@Injectable()
export class Area{
name:string;
currentArea:number = 1;
constructor(name:string) {
this.name = name;
}
public static createEmptyrea():Area{
return new Area("");
}
setArea(val:number) {
this.currentArea = val;
}
}
来自/ areas /
的传入json [
{
"name": "fun"
},
{
"name": "information"
}
]
答案 0 :(得分:5)
如果您正在使用observable,则不需要async/await
。您需要对mergeMap
使用rxjs@5
运算符,对flatMap
使用rxjs@4
:
getPosts(): Observable<Post[]> {
// subscribe until the area is available
return this.areaService.getAreas().mergeMap(area => {
// add authorization header with jwt token
let headers = new Headers();
headers.append('Authorization', 'token ' + this.authenticationService.token);
headers.append('Content-Type', 'application/json');
let options = new RequestOptions({
headers: headers
});
// use the area to get the response
return this.http.get('http://localhost:8000/areas/' + area.name + '/', options)
.map((response: Response) => response.json());
});
}
答案 1 :(得分:2)
您可以使用flatMap
或mergeMap
:
getPost(): Observable<Post[]> {
return this.http.get('http://localhost:8000/areas/', options)
.map((res: any) => res.json())
.flatMap((area: any) => {
return this.http.get('http://localhost:8000/areas/'+ area[0].name +'/', options)
.map((response: Response) => response.json());
.map((res: any) => res.json());
});
}