可观察的订阅返回值

时间:2018-08-14 13:52:19

标签: angular observable subscribe

我想有一个返回字符串的导出函数,但是我无法使它起作用,因为我在函数内部有一个订阅。

export function translateBreadcrumbSelf(key: string): string {
  this.translateService.get(key).subscribe(
    (result: string) => {
      return result;
    }
  );
}

如何返回结果字符串?我知道这是一个异步调用,所以有可能做到这一点吗?

1 个答案:

答案 0 :(得分:0)

在需要之前不要订阅。

// yourOtherFile
export function translateBreadcrumbSelf(key: string): string {
  this.translateService.get(key);
}

// thisFile
import { translateBreadcrumbSelf } from "yourOtherFile";
translateBreadcrumbSelf.subscribe(
    (result: string) => {
      console.log(result);
    }
  );
相关问题