没有 first on Api 方法,forkJoin 无法工作
forkJoin is not working without first on Api method
我已经写了forkJoin
,如图所示,如果我不将api方法设置为first
,below.It不起作用(也没有错误)。你能告诉我为什么?
my.ts
let getMyBookMarksApi = this.userService.getMyBookMarks(this.page);//needs first()
let getMyTopicsApi = this.userService.myTopic();//needs first()
Observable.forkJoin([getMyBookMarksApi, getMyTopicsApi])
.subscribe(res => {
this.setMyProfile(res[0]);
this.arrangeMyTopics(res[1]);
},
error => { },
() => { });
service.ts
myTopic() {
return this.api.get(config.myTopics).map((res: any) => res.json()).first();//without first forkjoin is not working.Why?
}
api.ts
get(api) {
return new Observable(observer => {
let header = new Headers();
this.createHeader(header)
.then(() => {
let options = new BaseRequestOptions();
options.withCredentials = true;
options.headers = header;
this.http.get(api, options)
.subscribe(response => {
observer.next(response);
}, (e) => {
observer.error(e);
});
})
})
}
Api 响应是这样的:
{
"num_of_definitions": 11,
"categories": [
{
"id": 68,
"name": "Founders",
"num_of_definitions": 1,
"icon": {
"large": ""
},
"from_color": "#1E3C72",
"to_color": "#2A5298"
},
{
"id": 27,
"name": "Innovation",
"num_of_definitions": 1,
"icon": {
"large": ""
},
"from_color": "#EE0979",
"to_color": "#FF6A00"
},
{
"id": 58,
"name": "Life success",
"num_of_definitions": 1,
"icon": {
"large": ""
},
"from_color": "#D53369",
"to_color": "#CBAD6D"
},
]
}
Forkjoin 的工作原理是并行订阅所有可观察对象,并且只有当它们全部完成后才会重新加入。这里的关键字是他们完成的时间。
运算符 .first() 从可观察对象中获取第一个发出的元素,然后完成。可能是因为您的源 observables 没有完成
为了完成示例,在 onNext
之后添加 onComplete 行
get(api) {
return new Observable(observer => {
let header = new Headers();
this.createHeader(header)
.then(() => {
let options = new BaseRequestOptions();
options.withCredentials = true;
options.headers = header;
this.http.get(api, options)
.subscribe(response => {
observer.next(response);
// add this line, it should trigger the completion of the outer observable
observer.complete();
}, (e) => {
observer.error(e);
});
})
})
}
我已经写了forkJoin
,如图所示,如果我不将api方法设置为first
,below.It不起作用(也没有错误)。你能告诉我为什么?
my.ts
let getMyBookMarksApi = this.userService.getMyBookMarks(this.page);//needs first()
let getMyTopicsApi = this.userService.myTopic();//needs first()
Observable.forkJoin([getMyBookMarksApi, getMyTopicsApi])
.subscribe(res => {
this.setMyProfile(res[0]);
this.arrangeMyTopics(res[1]);
},
error => { },
() => { });
service.ts
myTopic() {
return this.api.get(config.myTopics).map((res: any) => res.json()).first();//without first forkjoin is not working.Why?
}
api.ts
get(api) {
return new Observable(observer => {
let header = new Headers();
this.createHeader(header)
.then(() => {
let options = new BaseRequestOptions();
options.withCredentials = true;
options.headers = header;
this.http.get(api, options)
.subscribe(response => {
observer.next(response);
}, (e) => {
observer.error(e);
});
})
})
}
Api 响应是这样的:
{
"num_of_definitions": 11,
"categories": [
{
"id": 68,
"name": "Founders",
"num_of_definitions": 1,
"icon": {
"large": ""
},
"from_color": "#1E3C72",
"to_color": "#2A5298"
},
{
"id": 27,
"name": "Innovation",
"num_of_definitions": 1,
"icon": {
"large": ""
},
"from_color": "#EE0979",
"to_color": "#FF6A00"
},
{
"id": 58,
"name": "Life success",
"num_of_definitions": 1,
"icon": {
"large": ""
},
"from_color": "#D53369",
"to_color": "#CBAD6D"
},
]
}
Forkjoin 的工作原理是并行订阅所有可观察对象,并且只有当它们全部完成后才会重新加入。这里的关键字是他们完成的时间。 运算符 .first() 从可观察对象中获取第一个发出的元素,然后完成。可能是因为您的源 observables 没有完成
为了完成示例,在 onNext
之后添加 onComplete 行 get(api) {
return new Observable(observer => {
let header = new Headers();
this.createHeader(header)
.then(() => {
let options = new BaseRequestOptions();
options.withCredentials = true;
options.headers = header;
this.http.get(api, options)
.subscribe(response => {
observer.next(response);
// add this line, it should trigger the completion of the outer observable
observer.complete();
}, (e) => {
observer.error(e);
});
})
})
}