返回Observable以替换多个参数的值?

时间:2017-11-09 13:35:34

标签: angular observable

我正在返回一个带有3个(一个,两个,三个)参数的函数的Observable来替换配置URL。对于Ex。 http://www.example.com/status?a= {0}和b = {1}&安培; C = {2}

我的代码:

getLink(one string, two: string, three): Observable<any> {
    return Observable.of([this.externalLinks['url'].replace('\{0\}', one),
    this.externalLinks['url'].replace('\{1\}', two),
    this.externalLinks['url'].replace('\{2\}', three)]);
}

我无法返回多个值并替换参数。我想知道,如何返回所有三个值并替换配置URL。 我很感激任何线索...

1 个答案:

答案 0 :(得分:1)

实际上很简单(或者我弄错了):

getLink(one string, two: string, three): Observable<{
  url: string,
  replacedUrl: string,
  value1: string,
  value2: string,
  value3: string
}> {
  return Observable.of({
    url: this.externalLinks['url'],
    replacedUrl: this.externalLinks['url']
      .replace(`{0}`, one)
      .replace(`{1}`, two)
      .replace(`{2}`, three),
    value1: one,
    value2: two,
    value3: three,
  });
}

你的答案将是:

this.myService.getLink('one', 'two', 'three').subscribe(obj => {
  console.log(obj.url); // base URL
  console.log(obj.replacedUrl); // your URL completed
  console.log(obj.value1); // 'one'
  console.log(obj.value2); // 'two'
  console.log(obj.value3); // 'three'
});
相关问题