RXJS control observable invocation

前端 未结 2 1657
遇见更好的自我
遇见更好的自我 2021-01-04 05:07

I use RxJs version 5 within my Angular 2 project. I want to create some observables but I don\'t want the observables being invoked immediately.

In version 4 you co

2条回答
  •  佛祖请我去吃肉
    2021-01-04 05:37

    You can seperate the start of the observable from subscription to it by publishing the observable. The published observable will only be started after calling connect on it.

    Note that all subscribers will share a single subscription to the observable sequence.

    var published = Observable.of(42).publish();
    // subscription does not start the observable sequence
    published.subscribe(value => console.log('received: ', value));
    // connect starts the sequence; subscribers will now receive values
    published.connect();
    

提交回复
热议问题