【问题标题】:send subscription response as parameter to method Angular/rxJs将订阅响应作为参数发送到方法 Angular/rxJs
【发布时间】:2020-09-16 06:55:55
【问题描述】:

我的子组件中有如下订阅。

ngOnInit{
    this.subscription = this._cs.recieveData.subscribe((res) => {
    this.data= res;
  });
    UseDatafromSubs(this.data){
    //do Something
    }
}

我在上面的子组件中有另一个方法(UseDatafromSubs),它接受我的订阅响应作为参数:

在我的 ParentComponent 中,我正在更新如下服务:

senddata(){
     this._cs.send(sendData);
}

我能够从父级向子级发送和接收更新的数据,但是当新的订阅数据到来时,我的 useDataFromSubs 没有被调用,为了使用数据,它没有将订阅响应视为范围。我尝试使用如下添加方法:

 this.subscription = this._cs.recieveData.subscribe((res) => {
    this.data= res;
  }).add(()=>this.UseDatafromSubs(this.data));

但没用。

有人可以帮忙吗。

提前致谢。

【问题讨论】:

  • 听起来像是错误的模式。不要传递订阅,传递 observables。

标签: angular typescript rxjs subscription


【解决方案1】:

你能在订阅块中调用你想要的方法吗? 例如

ngOnInit{
    this.subscription = this._cs.recieveData.subscribe((res) => {
    this.data= res;
    this.UseDatafromSubs(res);
  })
}

【讨论】:

    猜你喜欢
    • 2017-12-16
    • 2019-03-16
    • 2023-03-12
    • 2021-11-04
    • 2021-12-19
    • 2020-05-23
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    相关资源
    最近更新 更多