RxJS - .subscribe() vs .publish().connect()

前端 未结 2 1399
忘掉有多难
忘掉有多难 2020-12-25 14:21

This is mainly an RxJs best practice/approach question, since my POC code works but I\'m brand new to RxJs.

The question boils down to .subscribe() vs <

2条回答
  •  心在旅途
    2020-12-25 14:39

    This sidesteps your question a bit but you may find it helpful:

    I would not return a different observable stream from the one that calls the http service because doing so makes it impossible for the calling function to:

    • cancel the stream
    • modify the stream
    • determine whether the operation was successful

    Instead I'd do:

    auth.servive.ts

    logout() : Observable  {
           return this.http.get(...).map(this.extractData)          
                .catch(this.handleError);
    }
    

    Now the calling code can do whatever it wants with the resulting url

    logout.component.ts

    logout(){
        this.authService.logout().subscribe(
            url => window.location.href = url,
            err => {
                /*todo: handle if error was thrown by authService.handleError*/
            }
        );
    }
    

提交回复
热议问题