首页 > 解决方案 > 如何将 HTTP 响应映射到用户定义的类型?

问题描述

// interface    
export interface IMovie {
    id: number;
    name: string;
    releaseDate: string;
    genre: string;
}

//component:

movie: IMovie = null;
  ngOnInit() {
    this.route.paramMap.subscribe((params: ParamMap) => {
      const movie_id = parseInt(params.get('movie_id'), 10);
      this.movieService.getMovieById(movie_id)
        .subscribe((movie) => this.movie = movie);
    });
  }

//service
getMovieById(movie_id: number): Observable<IMovie> {
    return this.http.get<IMovie>(url);
}

当我尝试在组件代码中打印时,我能够看到 Object ( console.log(movie)),但我不明白为什么IMovie没有发生映射,即 Object ->。

我尝试了不同的方式,但对我没有任何效果。

标签: angularangular-httpclientangular-observable

解决方案


推荐阅读