Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 在http解析时执行操作,并在需要时抛出错误_Angular_Http_Rxjs - Fatal编程技术网

Angular 在http解析时执行操作,并在需要时抛出错误

Angular 在http解析时执行操作,并在需要时抛出错误,angular,http,rxjs,Angular,Http,Rxjs,我正试图用可观测数据做以下工作,但我不知道最好的方法是什么: 我正在使用http调用登录端点。此服务将返回令牌。我想从应用程序中的两个不同点依次执行两个不同的操作:当http解析时,我需要存储提供的令牌,然后显示一条消息。我尝试了以下方法: return this.http.get(this.config.getApi('login'), params) .map(response => response.json()) .do((data) =

我正试图用可观测数据做以下工作,但我不知道最好的方法是什么:

我正在使用http调用登录端点。此服务将返回令牌。我想从应用程序中的两个不同点依次执行两个不同的操作:当http解析时,我需要存储提供的令牌,然后显示一条消息。我尝试了以下方法:

return this.http.get(this.config.getApi('login'), params)
           .map(response => response.json())
           .do((data) => {
               // Store token here
               // If it fails, throw error
               // Tried Observable.throw here but the subscriber doesn't get it.
            }).subscribe(
               (data) => {
                  // Everything went fine
                },
               () => {
                  // Either the http request failed or 
                 // the process of storing the token threw an error.
            }
       );
不幸的是,除非http调用失败,否则返回的observable的订阅者永远不会得到错误。我应该使用什么运算符来代替do

谢谢。

您应该订阅以访问响应

this.http.get(this.config.getApi('login'), params)
                .map(response => response.json())
                .subscribe(data=>{
                     console.log(data);
       });

也许是这样的

import { isDevMode } from '@angular/core';
import { _throw } from 'rxjs/observable/throw';


return this.http.get(this.config.getApi('login'), params)
.map(r => r.json())
.do(data => {
  this.token = data; //an instance variable defined in your service class
  if(isDevMode()) console.log(data) // display log only in devMode (why would you want to log something in console in production?! :D)
})
.catch(error => _throw(JSON.stringify(error));

Ofc您需要从外部组件、保护等调用此方法,以便执行http请求,据我所知,您希望这样做:

do http call 
    then do something else 
       if error   
           throw error
    subscribe 
        handle success
        handle error 
我能想到的一种方法是使用平面图。重写代码

return this.http.get(this.config.getApi('login'), params)
       .map(response => response.json())
       .flatMap((data) => {
           //do whatever you want to do , 
           // lets say error = true if something went wrong
           return error ? 
               Observable.throw('error message') :
               Observable.of(data);
        }).subscribe(...);

链接一个简单的例子

我想做的是运行代码来存储我的令牌,然后返回可观察的,以便其他人可以订阅。如果令牌存储操作失败,订阅该可观察对象的方法将获得错误。忘记将订阅添加到我的示例中。要将其存储在哪里?为什么?您要求在do中抛出一个错误,或者??然后您可以在DOT中添加条件。这正是它。非常感谢。