相关疑难解决方法(0)

是否有必要取消订阅由Http方法创建的可观察对象?

您是否需要取消订阅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)

memory-leaks rxjs angular2-http angular

181
推荐指数
8
解决办法
5万
查看次数

通过映射将类型(接口)强制转换为可观察

我一直在用Angular学习TypeScript。目前,我停留在这一刻。以前,我使用订阅方法,并且一切都可以正常工作,但不是我决定使用异步管道重写代码,但这只是行不通。

  1. RestAPI拒绝特定格式的请求

export interface responseFormat {
  success: boolean;
  data: any;
}
Run Code Online (Sandbox Code Playgroud)

  1. 返回的数据可以是不同的类型。在这种情况下,数据返回餐厅对象...也可能是不同对象的数组...

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)

  1. 以前,我使用以下代码(它可以正常工作):

/* 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)

observable typescript angular

4
推荐指数
1
解决办法
1万
查看次数