加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular – rxjs subject switchmap http错误

发布时间:2020-12-17 18:09:34 所属栏目:安全 来源:网络整理
导读:我正在使用Subject,switchMap和.next()在触发新的http.get()请求时取消先前挂起的http调用. 问题是当我处理http错误(如超时)时,在html中调用的方法_postMPCHC.next(…)不再起作用了… 我是否需要在出错时重新创建订阅?怎么样 ? import {Component} from '@
我正在使用Subject,switchMap和.next()在触发新的http.get()请求时取消先前挂起的http调用.

问题是当我处理http错误(如超时)时,在html中调用的方法_postMPCHC.next(…)不再起作用了…
我是否需要在出错时重新创建订阅?怎么样 ?

import {Component} from '@angular/core';
import {NavController,NavParams,Toast} from 'ionic-angular';
import {Http,URLSearchParams} from '@angular/http';
import {AppSettings} from '../../appSettings';
import {Subject} from 'rxjs/Subject';


@Component({
  templateUrl: 'build/pages/video/video.html'
})
export class VideoPage {
  _postMPCHC: any= new Subject();

  constructor(private http: Http,private nav: NavController) {
    this.defineHttp();
  }

  defineHttp() {

    var sub = this._postMPCHC.switchMap((x: string) => {
      let params: URLSearchParams = new URLSearchParams();
      params.set('token',AppSettings.API_TOKEN);
      params.set('prog','mhz');
      params.set('prog','mpchc');
      params.set('action',x);

      return this.http.get(AppSettings.API_ENDPOINT,{ search: params })

    }).timeout(5000,new Error('timeout exceeded')).subscribe(x => { },error => {
        let toast = Toast.create({
          message: 'Server response: ' + <any>error,duration: 3000,position: 'middle'
        });
        this.nav.present(toast);
      })

  }
}

解决方法

问题是当流出错时 – 它结束.如果要处理http错误但保持主流处于活动状态,则应该处理http流级别的错误.
请参阅此文章 – Error handling. What to do when error kills stream.

因此,在您的情况下,您可以通过添加.pipe(catchError(…))返回this.http.get(AppSettings.API_ENDPOINT,{search:params})行来处理错误 – 它将保持您的mai流活着并且将会捕获http错误

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读