我正在ngonit上订阅两个方法,但是另一个方法getTest()的结果来得很晚。我也在订阅其他方法,这些方法的结果显示在我下面共享的两种方法之间。您能帮我如何顺序地获得这两种方法的结果吗?
export class SomeComponent implements OnInit {
constructor(public _auth: SomeService, private _route: ActivatedRoute, private) {
ngOnInit() {
this._route.queryParams.subscribe(queryParam => {
console.log("Test,queryParam);
}); this.getTest();
}
getTest() {
return this.http.post("https://someexample.com", {}, {
params: parm
}).subscribe(data => {
console.log("Data", data.text());
});
}
}
答案 0 :(得分:1)
使用rxjs switchMap 链接多个订阅
import { HttpClient} from '@angular/common/http';
export class SomeComponent implements OnInit {
constructor(public _auth: SomeService, private http: HttpClient,
private _route: ActivatedRoute, private) {}
ngOnInit() {
this._route.queryParams.pipe(
switchMap((queryParam) => {
return this.getTest();
})
).subscribe( data => console.log("Data", data.text()));
}
getTest() {
return this.http.post("https://someexample.com", {}, { params: parm});
}
}
答案 1 :(得分:0)
您可以尝试以下方式:
export class SomeComponent implements OnInit {
constructor(public _auth: SomeService, private _route: ActivatedRoute, private) {
ngOnInit() {
this._route.queryParams.subscribe(queryParam => {
console.log("Test,queryParam);
this.getTest();
});
}
getTest() {
return this.http.post("https://someexample.com", {}, {
params: parm
}).subscribe(data => {
console.log("Data", data.text());
});
}
}