簡體   English   中英

如何從 rxjs 可觀察訂閱中調用 class 的方法

[英]How to call a method of a class from an rxjs observable subscription

請問:

此代碼工作正常:

class MyClass
{
  constructor()
  {
    this.m_observable = Rx.Observable.of(1, 2);
    
      this.m_observable.subscribe(
                                 function (x) {alert(x);}
                                 );
  }
  
  DoIt(x)
  {
    alert(5 * x);
  }
}

當我創建 class 的新實例時,我看到“1”然后是“2”。

我想在訂閱觸發時調用 class 的 DoIt 方法,此代碼不起作用,我看不到任何警報:

class rxjsShapeShifter
{
  constructor()
  {
    this.m_observable = Rx.Observable.of(1, 2);
    
      this.m_observable.subscribe(
                                 this.DoIt;
                                 );
  }
  
  DoIt(x)
  {
    alert(5 * x);
  }
}

如何從可觀察訂閱中調用該方法?

謝謝你的幫助。

我認為您的subscribe中有錯字。 您的第二個代碼應該是:

    constructor() {
      this.m_observable = Rx.Observable.of(1, 2);
      this.m_observable.subscribe(this.DoIt);
    }
    
    DoIt(x) {
      alert(5 * x);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM