您是否需要取消订阅Angular 2 http调用以防止内存泄漏?
fetchFilm(index) {
var sub = this._http.get(`http://example.com`)
.map(result => result.json())
.map(json => {
dispatch(this.receiveFilm(json));
})
.subscribe(e=>sub.unsubscribe());
...
Run Code Online (Sandbox Code Playgroud) 我一直在用Angular学习TypeScript。目前,我停留在这一刻。以前,我使用订阅方法,并且一切都可以正常工作,但不是我决定使用异步管道重写代码,但这只是行不通。
export interface responseFormat {
success: boolean;
data: any;
}Run Code Online (Sandbox Code Playgroud)
export interface restaurant {
_id: string;
name: string;
description: string;
images: file[];
cuisines: cuisine[];
blocked: boolean;
created: Date;
business_hours: object;
}Run Code Online (Sandbox Code Playgroud)
/* Provider */
getRestaurant(): Observable<responseFormat> {
return this.http.get<responseFormat>(environment.api + "/restaurant");
}
/* Component */
private restaurant: restaurant;
getRestaurant() {
this.restaurantProvider.getRestaurant().subscribe(res => {
if (res.success) this.restaurant = res.data;
});
}Run Code Online (Sandbox Code Playgroud)
结果是分配了变量的类型。...但是我不想保留订阅记录,因此我想使用Angular异步管道来转换代码...这就是我所做的... 它不起作用
/* Provider */
getRestaurant(): Observable<responseFormat> {
return this.http.get<responseFormat>(environment.api + "/restaurant");
}
/* Component …Run Code Online (Sandbox Code Playgroud)