在Angular工程中如何定义和使用自释放的基础组件?
定义:
base.component.ts
import { Subscription } from 'rxjs';
import { OnDestroy } from '@angular/core';
export class BaseComponent implements OnDestroy{
ngOnDestroy(): void {
for(let i = 0; i < this.subspritions.length; i ++) {
this.subspritions[i].unsubscribe();
}
}
subspritions: Subscription[] = [];
}
使用:
派生引用:
export class BoardComponent extends BaseComponent
收集:
this.subspritions.push(
dialogRef.afterClosed().subscribe(result => {
if (result && result.length > 0) {
this.subspritions.push(
this.boardService.addBoard(result).subscribe(x => {
// Send http request to add board
this.boards.push({ id: x.id, name: result });
this.currentBoard = x;
this.currentBoardId = x.id;
})
);
}
})
);
- 点赞
- 收藏
- 关注作者
评论(0)