简体   繁体   中英

Observer callback is not invoked when using rxjs interval

I have this method:

export class PeriodicData {
  public checkForSthPeriodically(): Subscription {
   return Observable.interval(10000)
    .subscribe(() => {
      console.log('I AM CHECKING');
      this.getData();
    });
  };

  public getData(): Observable<MyObject> {
    let objects: MyObject[] = this.filterData();
    return Observable.from(objects);
  }

  public filterData(): MyObject[] {
    let someData;
    // someData = filter(...) // logic to filter data
    return someData;
  }
}

Now I subscribe to getData() in another class:

class Another {
  constructor(private periodicData: PeriodicData ) {
    this.periodicData.getData().subscribe(obj => {
      console.log('IN ANOTHER CLASS');
    });
  }
}

This is not working. The "IN ANOTHER CLASS" is not being logged. Am I missing something ?

If you tested this only with live TypeScript transpiler, then it doesn't throw an error when you don't specifically include Observable and the from operator (even though I don't know why).

I added to the top of app.component.ts and it works now:

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/from';

See your demo on plnkr.co: http://plnkr.co/edit/uVnwG3bo0N8ZkrAgKp7F

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM