Reputation: 929
I noticed in my angular app that rxjs operators, like switchMap
, map
etc, were not working. (My IDE also gives a hint about that.) To be sure, I ran the following code. If I replace switchMap
with subscribe
, I do see 'value returned' in the console.
Any idea why the operators are not working?
import { Component, OnInit } from '@angular/core';
import {Observable} from "rxjs/Observable";
import 'rxjs/add/operator/switchMap';
@Component({
selector: 'page-2',
template: `
<h2>{{title}}</h2>
`,
})
export class Page2Component implements OnInit{
title = 'Page2';
ngOnInit() {
let obs = new Observable(observable => {
observable.next('value returned');
});
obs.switchMap(console.log);
}
}
Upvotes: 1
Views: 551
Reputation: 40647
In order to "fire" an observable you need to subscribe to it. Otherwise they will stay "cold".
The Subscribe operator is the glue that connects an observer to an Observable. In order for an observer to see the items being emitted by an Observable, or to receive error or completed notifications from the Observable, it must first subscribe to that Observable with this operator.
An Observable is called a “cold” Observable if it does not begin to emit items until an observer has subscribed to it; an Observable is called a “hot” Observable if it may begin emitting items at any time, and a subscriber may begin observing the sequence of emitted items at some point after its commencement, missing out on any items emitted previously to the time of the subscription.
Ref: http://reactivex.io/documentation/operators/subscribe.html
Upvotes: 2