Angular 搭运营商需要退订?

Angular Take operator needs unsubscribe?

我试图像这样过滤我的 angular material table,但它没有用(可能是因为它是一个 Observable)。

HTML

 <mat-form-field>
    <mat-label>Search</mat-label>
       <input matInput placeholder="Digite algo a ser apresentado (keyup)="toSearch($event.target)" />
    <mat-icon matSuffix>search</mat-icon>
</mat-form-field>

TS

aplicativos:any = this.aplicativoService.toGetAllAplicativos();
  dataSource = new MatTableDataSource();

  ngOnInit(): void {
    this.dataSource = this.aplicativos;
  }

  toSearch = (search: any) =>
    (this.dataSource.filter = search.value.trim().toLowerCase());

服务:

toGetAllAplicativos() { return this.http.get(`${this.API}/aplicativos`).pipe(take(1)); }

所以我将代码更改为这样工作,现在可以工作了:

TS

 aplicativos = this.aplicativoService.toGetAllAplicativos();
 dataSource;
 

  ngOnInit(): void {
     this.aplicativos.subscribe(response => {
         this.dataSource = new MatTableDataSource(response);
       })
    }
    
   toSearch(search: any){
      this.dataSource.filter = search.value.trim().toLowerCase();
    }

问题是:如果我使用 take(1),是否需要订阅?

不,如果您使用 take(1)first(),您不需要取消订阅,因为它会在收到第一个事件后自动取消订阅。

更详细的回答