我有一個組件可以創建3個獨立的http調用。一旦所有3個電話完成,我想執行一個操作(隱藏我的加載欄)。Angular2 RxJS - 多個HttpGet - 當它們全部完成時通知我
我認爲10.concat運算符是解決方案,但我無法解決如何使用它。我只希望我的http調用執行一次,但因爲我有多個.subscribe()調用(原始調用和concat),所以我明白爲什麼我的http調用是以2X進行的。我只是不知道如何編碼。也許我需要forkJoin?.Concat?
我目前Angular2 Component.ts - 使得HTTP調用2X
const form$ = this.myService.getSubmissionForm(this.submissionId);
this.subscription = form$
.subscribe((submissionForm: SubmissionForm) => {
this.submissionForm = submissionForm;
},
(error: any) => { }, /* Error */
() => { }); /* Complete success*/
const comments$ = this.myService.getComments(this.submissionId);
this.subscription = comments$.subscribe(
(comments: Comment[]) => {
this.submissionComments = comments;
});
const documents$ = this.myService.getRelatedDocuments(this.submissionId);
this.subscription = documents$.subscribe(
(viewModel: SubmissionDocumentsSection) => {
this.submissionDocumentSection = viewModel;
});
// Inform any listeners that our async calls have all completed.
const combined$ = Observable.concat(form$, comments$, documents$);
combined$.subscribe(
() => { },
(error: any) => {
this.ngInitLoadComplete.emit(false);
},
() => {
this.ngInitLoadComplete.emit(true);
});
我想我Want.ts
const form$ = this.myService.getSubmissionForm(this.submissionId);
const comments$ = this.myService.getComments(this.submissionId);
const documents$ = this.myService.getRelatedDocuments(this.submissionId);
// Inform any listeners that our async calls have all completed.
const combined$ = Observable.concat(form$, comments$, documents$);
/** Bad syntax here! How do I subscribe and get results? **/
combined$.subscribe(
(submissionForm: SubmissionForm ,
comments: Comment[],
viewModel: SubmissionDocumentsSection ) => {
this.submissionForm = submissionForm;
this.submissionComments = comments;
this.submissionDocumentSection = viewModel;
},
(error: any) => {
this.ngInitLoadComplete.emit(false);
},
() => {
this.ngInitLoadComplete.emit(true);
});
我的語法是不正確的,我有麻煩尋找解決方案。 我該如何做到這一點?
謝謝 - 我忘了補充一點,我仍然想知道每個單獨的observable何時完成。所以一旦submitForm完成,我想捕獲它並執行一個動作,同時等待其他人完成。可能? – TheOneAndOnlyNobody
這使我回到我原來的問題 - 重複http請求,這是多個訂閱的結果。 – TheOneAndOnlyNobody
在我的情況下,避免重複呼叫更重要,然後通知每個人完成,因此我會將您的原始答案標記爲正確.ForkJoin消除重複呼叫。您可以刪除**編輯**,因爲它不能解決問題。感謝您的幫助。 – TheOneAndOnlyNobody