【发布时间】:2017-07-03 11:46:25
【问题描述】:
从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()是否也在使用 observable?如果是,您需要使用.mergeMap()运算符将 get_categories() 中的 observable 链接到 send_categories() 中的那个。如果您需要语法方面的帮助,请告诉我。 -
send_catagories() 没有使用 observable,请告诉我语法:return this.http.post( url, body, {headers: headers, withCredentials:true}) .subscribe( response => { this.total_page = response.json();返回 this.total_page; }, .share() ); } 然后 this.get_category(1).subscribe(response=> { this.callfunc(); });
-
知道了。我已经用正确的语法发布了一个答案。
标签: javascript angular typescript promise subscribe