【问题标题】:1 errors occurred during unsubscription1 退订时发生错误
【发布时间】:2021-04-14 23:55:39
【问题描述】:

当我每秒调用一个请求时,我会收到此错误:

core.js:5967 ERROR TypeError: Cannot read property 'remove' of null

在 LoadingBackdropService.hide (loading-backdrop.service.ts:18)

在 TapSubscriber._tapNext (payment.component.ts:57)

在 TapSubscriber._next (tap.js:40)

在 TapSubscriber.next (Subscriber.js:49)

在 SwitchMapSubscriber.notifyNext (switchMap.js:70)

在 InnerSubscriber._next (InnerSubscriber.js:11)

在 InnerSubscriber.next (Subscriber.js:49)

在 MapSubscriber._next (map.js:35)

在 MapSubscriber.next (Subscriber.js:49)

在 FilterSubscriber._next (filter.js:33)

如何纠正错误?

ts:

      ngUnsubscribe: Subscription;
    
      ngOnInit() {
        this.load();
      }
    
      load() {
        this.loadingBackdrop.show();
        this.ngUnsubscribe = timer(0, 1000).pipe(
          switchMap(() => this._object.get(this.card.card_id)
            .pipe(finalize(() => this.loadingBackdrop.hide())))
        ).subscribe(data => this.dataSource = new MatTableDataSource(data));
      }
    
      ngOnDestroy() {
        if (this.ngUnsubscribe !== undefined) {
          this.ngUnsubscribe.unsubscribe();
        }
      }

加载背景.service.ts:

  show() {
    const backdropWrapperElement = this.createLoadingBackdropTemplate();
    const bodyElement = document.querySelector('body');
    bodyElement.appendChild(backdropWrapperElement);
  }

  hide() {
    const backdropWrapperElement = document.querySelector('#loadingBackdrop');
    backdropWrapperElement.remove();
  }

  private createLoadingBackdropTemplate(): HTMLDivElement {
    const element = document.createElement('div');
    element.setAttribute('id', 'loadingBackdrop');
    element.setAttribute('style', `
      width: 100vw;
      height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
      position: absolute;
      top: 0;
      z-index: 999;
    `);
    element.innerHTML = `
      <div class="backdrop"></div>
      <div class="spinner">
        <div></div>
        <div></div>
        <div></div>
        <div></div>
      </div>
    `;
    return element;
  }

【问题讨论】:

  • 您能否提供更多代码或可能的 stackblitz 可行演示? ?我认为问题可能出在loadingBackdrop,但没有办法检查出来
  • 问题是该元素没有loadingBackdrop ID,不是吗?你也可以分享createLoadingBackdropTemplate()函数吗?
  • 请提供createLoadingBackdropTemplate()(或者更确切地说是创建的相应dom元素)。 document.querySelector 没有找到您想要的(可能缺少达里奥提到的 id)。
  • 当你在浏览器中检查dom时,元素是否真的存在?
  • Gunnar 说得对,我认为 100% 的问题只是 document.querySelector 返回 null 你需要确保元素存在并且它被赋予了正确的 id

标签: angular typescript


【解决方案1】:

错误的根源似乎来自这一行

.pipe(finalize(() => this.loadingBackdrop.hide()))) 

特别是在 hide 方法内部,确保 loadingBackdrop 对象被正确实例化。

【讨论】:

    【解决方案2】:

    一个简单的解决方案是使用takeUntil 运算符

    import { Subject } from 'rxjs'
    import { takeUntil, tap } from 'rxjs/operators'
      destroyed$ = new Subject();
      ngOnInit() {
        this.load();
      }
    
      load() {
        this.loadingBackdrop.show();
        timer(0, 1000).pipe(
          switchMap(() => this._object.get(this.card.card_id)),
          tap(() => this.loadingBackdrop.hide()),
          takeUntil(this.destroyed$)
        ).subscribe(data => this.dataSource = new MatTableDataSource(data));
      }
    
      ngOnDestroy() {
        this.destroyed$.next()
      }
    

    这个想法是,只要destroyed$ 未被触发,那么Observable 流将处于活动状态,但一旦destroyed$ Observable 不为空,angular 将自动取消订阅 Observable

    我也将finalize 更改为tap。 finalize 将在流完成后执行,但 tap 将进入可观察流

    【讨论】:

    • 我还是有错误
    • 请检查一下,我对代码做了一点改动
    • 如果这样做,它不会执行finalize (() =&gt; this.loadingBackdrop.hide())
    • 这与实际问题无关。
    【解决方案3】:

    尝试将其更改为:

    if (this.ngUnsubscribe !== undefined && this.ngUnsubscribe !== null) {
        this.ngUnsubscribe.unsubscribe();
    }
    

    【讨论】:

    • 有什么用?查看我在load() 中的完整请求
    • 当有分号时你不能做 .pipe...如果你链调用分号会破坏它。
    • 就像这样写:this.loadingBackdrop;.show()...也不行,因为有分号。分号总是结束行/命令,如果你想继续访问前一行的内容,你必须删除分号
    • 另外......加载函数中没有分号......这就是它工作但另一个没有的原因
    • 你不懂我。我从加载函数中提取了这部分
    猜你喜欢
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    相关资源
    最近更新 更多