“预订”类型的参数不能分配给“字符串”类型的参数。角度的

时间:2020-10-19 08:11:41

标签: angular

这是我的Subscription变量,可从配置设置获取api

public ChannelsAPI=this._configservice.getConfiguration("ChannelAPI").subscribe((result) => console.log(result));

这是我的_Configservice.getConfiguration方法

  getConfiguration(key) {
    return this._http.get('./assets/config/' + this._env + '.json').map(res => {
        this.result = res.json();
        return this.result[key];
    });
}

这是我遇到错误的地方
this.ChannelsApi可疑字符串,但是正在订阅,这就是我收到错误的原因

  getChannels(): Observable<Channeldata[]> {
    return this.httpClient.get<Channeldata[]>(this.ChannelsAPI)      >>>>  *ERROR PLACE*
    .pipe(
      catchError(this.errorHandler)
    )
  }

2 个答案:

答案 0 :(得分:0)

您需要用aee865d1-7253-489c-8d53-2a0d580639d0替换this.httpClient.get<Channeldata[]>(this.ChannelsAPI),因为您已经在此变量中准备了整个请求。 this.ChannelsAPI中的get方法需要传递一个字符串。

答案 1 :(得分:0)

httpClient的get方法采用一个字符串,getConfiguration返回一个可观察到的字符串。目前,您实际上是将channelApi设置为预订的结果,因此您收到错误,无法将预订分配给字符串。要解决此问题,您可以通过2种方法在订阅中选择设置channelsApi的路线

this._configservice.getConfiguration("ChannelAPI")
.subscribe((result) => this.channelsApi = result);

或者,如果您愿意多次执行此步骤,则可以在获取频道中进行切换映射。

getChannels(): Observable<Channeldata[]> {
    return this._configservice.getConfiguration("ChannelAPI")
    .pipe(
      switchMap((r) => this.httpClient.get<Channeldata[]>(r))
      catchError(this.errorHandler)
    )
  }