首页 > 解决方案 > 在http get请求Angular 6中返回字符串

问题描述

return我的请求中有一些问题。

getTest(id: string): string {
  let foo: string = '';
  this.service.testProfile(id).subscribe(
    response => {
      foo = response.body.foo;
    },
    error => {
      console.log(error);
    }
  )
  return foo;
}

我想开始foo我的回应。foo然后只是返回 foo 的新值。一切正常,但没有结果,知道我做错了什么吗?

感谢

标签: angularhttpangular6

解决方案


我用 Promise 解决了

getTest(id: string): Promise<any>  {
 return new Promise((resolve,reject) => {
  this.service.testProfile(id).subscribe(
   response => {
    foo = response.body.foo;
   },
   error => {
    console.log(error);
   }
   )
  }
  )
 }

谢谢大家 !


推荐阅读