账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Angular4中使用rxjs, http请求多次发送
    25
    0

    在Angular4.x中使用Rxjs请求数据,发现多次请求会造成n+1次请求次数,极大地影响性能,目前只能采用最笨拙的依次声明对应Subscription对象一一手动取消订阅,感觉不是正常办法,想请教更好的解决方式,示例代码如下:

    service:

    getRuleHitList(): any {
          const formData = {
            'uuid': this.orderNo
          };
          this.$http
            .post(this.HOST.host + '/api/bqsBlackList', formData)
            .subscribe(res => {
              console.log(res);
              this.RuleHitSubject.next(res);
            })
        }

    component:

    ruleSubscription: Subscription;
    
    getRule() {
        this.service.getRuleHitList();
        this.ruleSubscription = this.service.RuleHitSubject.subscribe(res => {
            if (res && res.code === '0') {
              if (res.strategySet || res.Rule) {
                this.ruleListFlag = true;
                this.ruleList = res;
              }
            } else {
              this.ruleListFlag = false;
            }
            this.ruleSubscription.unsubscribe()
        })

    上述getRule会在当前页面多次用到,且component不会销毁,因此不加unsubscribe()手动取消订阅页面会越来越卡,总感觉自己用的不对,请大佬不吝赐教;

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 青丝缠雪 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部