繁体   English   中英

等待Ionic中的订阅方法

[英]Wait for a subscribe method in Ionic

我在提供程序中有一种方法

getProfile() {
    let headers = new Headers();
    this.loadToken();
    headers.append('Authorization', this.authToken);
    headers.append('Content-Type','application/json');
    return this.http.get(this.db_url + 'profile',{headers: headers})
      .map(res => res.json());
  }

我在另一个提供商中调用上述方法

getProfile() {
    this.authService.getProfile().subscribe((profile: any) => {
      this.profile = profile.user._id;
    },(err : any) => console.log(err))
  } 

在这里我要停止执行代码,直到执行this.profile = profile.user._id这行。在声明getProfile()方法的同一provider中,我将http请求作为

let headers = new Headers();
    headers.append('Content-Type','application/json');
    let selected = {
      address: home.address, 
      date: new Date(),
      id: this.profile // from the getProfile() file.
    }

console.log(selected);

id的输出是undefined 我认为这里存在异步。

将此代码放在类似的函数中

setHeadres(){
 let headers = new Headers();
 headers.append('Content-Type','application/json');
 let selected = {
  address: home.address, 
  date: new Date(),
  id: this.profile // from the getProfile() file.
 }
}

并在getProfile()函数内调用此函数,如下所示

getProfile() {
 this.authService.getProfile().subscribe((profile: any) => {
   this.profile = profile.user._id;
   this.setHeaders();
 },(err : any) => console.log(err))
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM