【问题标题】:Observable subscribe return value可观察的订阅返回值
【发布时间】:2018-08-14 13:52:18
【问题描述】:

我想要一个返回字符串的导出函数,但我不能让它工作,因为我在函数内部有一个订阅。

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

如何返回结果字符串?我知道这是一个异步调用,所以甚至可以这样做吗?

【问题讨论】:

标签: angular observable subscribe


【解决方案1】:

在需要之前不要订阅。

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

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

【讨论】:

    猜你喜欢
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 2018-03-27
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    相关资源
    最近更新 更多