【发布时间】:2021-07-21 22:50:16
【问题描述】:
有没有办法取消订阅来自角度服务 onDestroy 钩子的 http 调用。
附:我知道 rxjs 'take' 运算符或取消订阅组件本身的服务。
import { HttpClient } from "@angular/common/http";
import { Injectable, OnDestroy } from "@angular/core";
@Injectable()
export class SampleService implements OnDestroy {
constructor(private httpClient: HttpClient) {}
getSampleData() {
return this.httpClient.get("https://jsonplaceholder.typicode.com/todos/1");
}
ngOnDestroy(): void {
// automatically unsubscribe to service.
}
}
【问题讨论】:
-
@R.Richards 感谢您的链接。但是当服务实例被组件销毁时,我们是否可以编写一些清理逻辑来检查是否需要取消订阅任何 http 调用??
标签: javascript angular typescript rxjs angular-material