1

我正在尝试测试此组件的 ngOnDestroy() 方法,但无法找到正确的方法。正确的方法应该是什么?

import {Component, OnDestroy} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {Subject} from 'rxjs';
import {takeUntil} from 'rxjs/operators';


@Component({.......})
export class MyComponent implements OnDestroy {

  private readonly destroyed = new Subject();

  constructor(private readonly route: ActivatedRoute) {
    this.route.queryParams.pipe(takeUntil(this.destroyed)).subscribe(params => {
      // custom code here
    });
  }

  ngOnDestroy() {
    this.destroyed.next();
    this.destroyed.complete();
  }
}
4

0 回答 0