简体   繁体   English

错误TS2322:类型'对象'不能分配给'电影'类型? Angular 7

[英]error TS2322: Type 'Object' is not assignable to type 'Movie'? Angular 7

I'm sorry to distract with my stupid question. 我很抱歉分散了我的愚蠢问题。 I recently just study Angular 2+ and still do not understand everything. 我最近刚学习Angular 2+,但仍然不了解一切。 I looked at other answers to this question, but they did not help me. 我看了这个问题的其他答案,但他们没有帮助我。 I have a function that returns a list and a separate item of the list. 我有一个函数返回列表和列表的单独项。

Code from Service: 服务代码:

constructor(private http: HttpClient) { }
getMovies() {
 return this.http.get(this.baseUrl);
}
getMovie(id: number) {
return this.http.get(this.baseUrl + id);
}

The component that returns lists 返回列表的组件

 private movies = [];
 constructor(private movieService: MovieService) {}
 ngOnInit() {
  this.getMovies();
 }
private getMovies() {
 this.movieService.getMovies()
  .subscribe((response: any) => {
    this.movies = response.results;
  });
}

And my component which returns a list item: 我的组件返回一个列表项:

movie = new Movie();

constructor(
 private movieService: MovieService,
 private activeRoute: ActivatedRoute) {
}

ngOnInit(): void {
 this.getMovie();
}

getMovie(): void {
 const id = +this.activeRoute.snapshot.paramMap.get('id');
  this.movieService.getMovie(id)
  .subscribe(movie => this.movie = movie);
}

Whatever I do and how I did not rewrite the code, I get the same error. 无论我做什么以及如何不重写代码,我都会得到同样的错误。

ERROR in src/app/page/movie-detail/movie-detail.component.ts(34,27): error TS2322: Type 'Object' is not assignable to type 'Movie'. src / app / page / movie-detail / movie-detail.component.ts(34,27)中的错误:错误TS2322:类型“对象”不能指定为“电影”类型。 The 'Object' type is assignable to very few other types. “对象”类型可分配给极少数其他类型。 Did you mean to use the 'any' type instead? 您的意思是使用“任何”类型吗? src/app/page/movie-detail/movie-detail.component.ts(34,27): error TS2322: Type 'Object' is not assignable to type 'Movie'. src / app / page / movie-detail / movie-detail.component.ts(34,27):错误TS2322:类型“对象”不能指定为“电影”类型。 The 'Object' type is assignable to very few other types. “对象”类型可分配给极少数其他类型。 Did you mean to use the 'any' type instead? 您的意思是使用“任何”类型吗? Property 'page' is missing in type 'Object'. “对象”类型中缺少属性“页面”。

How to correct this error, or at least where you can read about it, see how it is done? 如何纠正这个错误,或者至少在你能读到它的地方,看看它是如何完成的? I have an API request which returns the following: 我有一个API请求,返回以下内容:

{"page": 1,
"results": [
{
  "poster_path": "/9O7gLzmreU0nGkIB6K3BsJbzvNv.jpg",
  "adult": false,
  "overview": "Framed in the 1940s for the...",
  "release_date": "1994-09-10",
  "genre_ids": [
    18,
    80
  ],
  "id": 278,
  "original_title": "The Shawshank Redemption",
  "original_language": "en",
  "title": "The Shawshank Redemption",
  "backdrop_path": "/xBKGJQsAIeweesB79KC89FpBrVr.jpg",
  "popularity": 5.446135,
  "vote_count": 5250,
  "video": false,
  "vote_average": 8.32
},
{
  "poster_path": "/lIv1QinFqz4dlp5U4lQ6HaiskOZ.jpg",
  "adult": false,
  "overview": "Under the direction o...",
  "release_date": "2014-10-10",
  "genre_ids": [
    18,
    10402
  ],
  "id": 244786,
  "original_title": "Whiplash",
  "original_language": "en",
  "title": "Whiplash",
  "backdrop_path": "/6bbZ6XyvgfjhQwbplnUh1LSj1ky.jpg",
  "popularity": 9.001948,
  "vote_count": 2072,
  "video": false,
  "vote_average": 8.28
}
],
"dates": {
"maximum": "2016-09-01",
"minimum": "2016-07-21"
},
"total_pages": 33,
"total_results": 649
}

I think I have error in service, because I do not get the pages when I display the list and a separate item of the list. 我认为我的服务有错误,因为当我显示列表和列表的单独项目时,我没有得到页面。 But I did not find how to do it differently, or these options are no longer working. 但我没有找到如何以不同方式做到这一点,或者这些选项不再有效。

you can either specify your type in your get function 您可以在get函数中指定类型

return this.http.get<Movie>(this.baseUrl);

Or specify it in your function 或者在您的函数中指定它

getMovies(): Observable<Movie> {
 return this.http.get(this.baseUrl);
}

Or do both 或两者都做

 getMovies(): Observable<Movie> {
     return this.http.get<Movie>(this.baseUrl);
}

因为您正在使用HttpClient ,所以您必须提供返回结果的必要转换, 如此答案所示,其中显示了如何设置组件和服务提供程序以调用和接收http JSON结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误TS2322:无法将类型“对象”分配给类型“接触”。 角 - error TS2322: Type 'Object' is not assignable to type 'Contact'. Angular Angular:错误TS2322:类型'ItemsResponse'不能赋值为'string []' - Angular: error TS2322: Type 'ItemsResponse' is not assignable to type 'string[]' 错误 TS2322:类型“数字”不可分配给 angular 中的类型“字符串” - error TS2322: Type 'number' is not assignable to type 'string' in angular Angular - 错误 TS2322:类型 'null' 不可分配给类型 'string' - Angular - error TS2322: Type 'null' is not assignable to type 'string' 错误 TS2322:“对象”类型不可分配给“产品”类型 - Error TS2322: Type 'Object' is not assignable to type 'Produit' 角度:错误TS2322:类型&#39;Observable &lt;{}&gt;&#39;不可分配...使用share()运算符 - Angular: error TS2322: Type 'Observable<{}>' is not assignable … with share() operator Angular TS2322:类型“字符串”不可分配给类型“IHamster []” - Angular TS2322: Type 'string' is not assignable to type 'IHamster[]' TS2322:类型“_”不可分配给类型“从不”。 angular - TS2322: Type '_' is not assignable to type 'never'. angular 错误 TS2322:“数字”类型不可分配给“字符串”类型 - error TS2322: Type 'number' is not assignable to type 'string' 错误TS2322:类型&#39;Observable &lt;{} []&gt;&#39;不能分配给&#39;Observable类型 <Archive[][]> “ - error TS2322: Type 'Observable<{}[]>' is not assignable to type 'Observable<Archive[][]>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM