我需要調用一個方法後,從HTTP POST請求獲取數據角2:如何調用一個函數得到響應後訂閱http.post
服務:request.service.TS
get_categories(number){
this.http.post(url, body, {headers: headers, withCredentials:true})
.subscribe(
response => {
this.total = response.json();
}, error => {
}
);
}
組件:categories.TS
search_categories() {
this.get_categories(1);
//I need to call a Method here after get the data from response.json() !! e.g.: send_catagories();
}
只有當我改變工作:
服務:request.service.TS
get_categories(number){
this.http.post(url, body, {headers: headers, withCredentials:true})
.subscribe(
response => {
this.total = response.json();
this.send_catagories(); //here works fine
}, error => {
}
);
}
但我需要調用this.get_categories(1);
後要調用的方法send_catagories()
構件的內部這樣
組件:categories.TS
search_categories() {
this.get_categories(1);
this.send_catagories(response);
}
我做錯了什麼?
'send_catagories()'還使用可觀察?如果是,則需要使用'.mergeMap()'運算符將get_categories()中的observable鏈接到send_categories()中的observable。讓我知道你是否需要語法幫助。 – AngularChef
send_catagories()不使用observable,請告訴我語法:return this.http.post(url,body,{headers:headers,withCredentials:true}) .subscribe( response => {this.total_page = response .json(); return this.total_page; },.share() ); } then this.get_category(1).subscribe(response => {this.allFunc(); }); – Louis
明白了。我用正確的語法發佈了一個答案。 – AngularChef