从可观察到的字符串值

时间:2019-09-10 12:02:34

标签: angular rxjs observable

我具有带有Observable的此功能,因此我可以使用文件的内容。

  readDocument(fileChangeEvent: Event) {
    return new Observable(obs=>{
        const file = (fileChangeEvent.target as HTMLInputElement).files[0];
        let fileReader = new FileReader();
        fileReader.onload = (e) => {
          obs.next(fileReader.result);
        }
        fileReader.readAsText(file);  
    })
}

我使用该功能将csv文件的内容作为字符串获取。

    this.readDocument(data).subscribe(documentvalue=>{
         var teststring = documentvalue;
      })

我的问题是,如果我然后尝试使用字符串(在我的示例中为高图表):

  this.readDocument(data).subscribe(documentvalue=>{
    self.chartOptions.data.csv = documentvalue;
  })

我收到以下错误:

  

类型'Subscription'不能分配给类型'string'。

如何从订阅/可观察值中获取纯String值?

1 个答案:

答案 0 :(得分:1)

我得到的错误是using (var client = _httpClientFactory.CreateClient()) { client.BaseAddress = new Uri("https://example.com/authenticate"); var response = await client.PostAsync(string.Empty, new JsonContent(data)); var content = await response.Content.ReadAsStringAsync(); return StatusCode((int)response.StatusCode, content); } ,它比您得到的错误更有意义。

由于可观察对象中没有类型信息,因此默认情况下会认为可观察对象会发出空对象Type '{}' is not assignable to type 'string'。不能将其分配给字符串{}。要解决此问题,您需要为可观察对象显式提供类型参数,并确保它发出字符串。这是修复此问题的代码:

self.chartOptions.data.csv