How to create an RxJS theme in Angular 2?

I can create an Observable in my angular component below the path

... ... import { Observable } from 'rxjs/Observable'; .. ... let observable = new Observable( function subscribe(observer) { observer.next(1); observer.next(2); observer.next(3); observer.next(4); setTimeout(() => {observer.next(5); },9000); }); 

But I do not know how to create a theme, can someone give an example for the same?

+5
source share
2 answers

Using Rx.Subject with Angular, it is no different from using it without Angular, the basic principle is the same, see example below

 const subject = new Rx.Subject(); const subscription = subject.subscribe( (data) => console.log(data), (err) => console.log(err), () => console.log('Completed') ); subject.next(1); subject.next(2); subject.next(3); subject.complete(); 
 <script src="https://unpkg.com/@reactivex/ rxjs@5.0.0-beta.7 /dist/global/Rx.umd.js"></script> 

You need to create a new instance of Rx.Subject , then subscribe to it where you need it and fire the event.

+2
source

Here's the plunker: http://plnkr.co/edit/jLtFLjWrvHGI0ZPHl10B?p=preview

 import {Subject} from 'rxjs/Subject'; @Injectable() export class MyService { public invokeEvent:Subject<any> = new Subject(); constructor() {} } 

subscribe to it as an observable:

  constructor(private _myService: MyService) { this._myService.invokeEvent.subscribe((value) => { console.log(value); }); } 

and click the values ​​for it, as observed again with the following

  ngAfterViewInit(){ this._myService.invokeEvent.next(1); } 
+10
source

Source: https://habr.com/ru/post/1259327/


All Articles