简体   繁体   English

已解决 Angular 标识符“标题”未定义。 'Movie[]' 不包含这样的成员

[英]Solved Angular Identifier 'title' is not defined. 'Movie[]' does not contain such a member

i'll trying to get data from backend via angular services.我将尝试通过 angular 服务从后端获取数据。
i read angular docs and they said that we have to use interfaces when we want to send request to somewhere我读了 angular 文档,他们说当我们想向某个地方发送请求时我们必须使用接口
something like this:像这样的东西:

return this.http.get<Movie[]>(this.movieAPI)

so after that i tried to Subscribe to that service, everything works fine but i also got this error from my compiler:所以在那之后我尝试订阅该服务,一切正常,但我的编译器也收到了这个错误:

ERROR in app/movie/movie-detail/movie-detail.component.html:4:12 - error TS2339: Property 'title' does not exist on type 'Movie[]'.

its like my interface doesn't know the properties就像我的界面不知道属性
this are my files这是我的文件

movie.ts ( interface )电影.ts(界面)

export interface Movie {
  id: number
  title: string
}

movie.service.ts电影服务.ts

import { Injectable } from '@angular/core'
import { HttpClient } from '@angular/common/http'
import { Observable } from 'rxjs'
import { Movie } from './movie'

@Injectable({
  providedIn: 'root'
})
export class MovieService {
  movieAPI = '../../assets/movie.json' // mock data
  api = '' // i removed my api 

  constructor(
    private http: HttpClient
  ) {}

  getMovies(): Observable<Movie[]> {
    return this.http.get<Movie[]>(this.movieAPI)
  }

  searchMovie(params?: string): Observable<Movie[]> {
    return this.http.get<Movie[]>(`${this.api}/${params}`)
  }
}

movie.component.ts电影组件.ts

import { Component, OnInit } from '@angular/core'
import { ActivatedRoute } from '@angular/router'
import { MovieService } from '../../movie/movie.service'
import { Movie } from '../../movie/movie'

@Component({
  selector: 'app-movie-detail',
  templateUrl: './movie-detail.component.html',
  styleUrls: ['./movie-detail.component.scss']
})
export class MovieDetailComponent implements OnInit {
  params: string
  movie: Movie[]

  constructor(
    private activatedRoute: ActivatedRoute,
    private movieSerivce: MovieService
  ) {
    this.getParams()
  }

  ngOnInit(): void {
    this.getDetail()
  }

  getDetail(): void {
    this.movieSerivce.searchMovie(this.params)
      .subscribe(
        (data:Movie[]) => this.movie = data
      )
  }

  getParams(): void {
    this.params = this.activatedRoute.snapshot.paramMap.get('id')
  }

}

movie.component.html电影.component.html

<h1>
  {{ movie?.title }}
</h1>
{{ movie | json }}

[Updated] [更新]
in movie.service.ts在电影.service.ts

...
searchMovie(params?: string): Observable<Movie> {
    return this.http.get<Movie>(`${this.api}/${params}`)
  }
...

in movie-detail.component.ts在movie-detail.component.ts

...
getMovie(): void {
    const id = +this.activatedRoute.snapshot.paramMap.get('id')
    this.movieSerivce.searchMovie(`${id}`)
      .subscribe(
        (data:Movie) => this.movie = data
      )
}
...

The interface Movie has the title property but Movie[] does not.接口Movie具有title属性,但Movie[]没有。

In your code this.movie is of type Movie[] meaning it is an array.在您的代码this.movieMovie[]类型,这意味着它是一个数组。

The following will work:以下将起作用:

 <h1>
   {{ movie?[0].title }}
 </h1>

If you'll change the naming from movie: Movie[] to movies: Movie[] it will be more intuitive.如果您将命名从movie: Movie[]更改为movies: Movie[]会更直观。

You can loop the array with ngFor :您可以使用ngFor循环数组:

 <h1 *ngFor="let movie of movies">
   {{ movie.title }}
 </h1>

If getDetail should return only a single movie change the expected returned object like this:如果getDetail应该只返回单个电影更改,则预期返回的 object 如下所示:

 movie: Movie;  

 getDetail(): void {
    this.movieSerivce.searchMovie(this.params)
      .subscribe(
        (data:Movie) => this.movie = data
      )
  }
  searchMovie(params?: string): Observable<Movie[]> {
    return this.http.get<Movie>(`${this.api}/${params}`)
  }

In your service the http request returns an array not a single object.在您的服务中,http 请求返回一个数组,而不是单个 object。 Therefore the from the array you can't access inner properties.因此,您无法从数组访问内部属性。 If the service has to return an array you can to the following如果服务必须返回一个数组,你可以到以下

use ngfor使用 ngfor

<h1 *ngFor="let oneMovie movie">
  {{ oneMovie .title }}
</h1>

暂无
暂无

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

相关问题 未定义标识符“标题”。 &#39;{}&#39; 不包含这样的成员 angular 8 - Identifier 'title' is not defined. '{}' does not contain such a member angular 8 未定义标识符“标题”。 &#39;__object&#39;不包含这样的成员 - Identifier 'title' is not defined. '__object' does not contain such a member Angular:标识符“标题”未定义。 'never' 不包含这样的成员。 异常问题 - Angular: Identifier 'title' is not defined. 'never' does not contain such a member. Unusual problem 未定义标识符“ X”。 “ Y”不包含此类成员Angular - Identifier 'X' is not defined. 'Y' does not contain such a member Angular 未定义角度标识符“ playerType”。 &#39;&#39;不包含这样的成员 - Angular Identifier 'playerType' is not defined. '' does not contain such a member 未定义标识符“ ...”。 &#39;__type&#39;不包含这样的成员Angular FormArray - Identifier '…' is not defined. '__type' does not contain such a member Angular FormArray 未定义标识符“长度”。 &#39;null&#39; 不包含这样的成员 ng(0) - Identifier 'length' is not defined. 'null' does not contain such a member ng(0) 未定义标识符“必需”。 &#39;__type&#39; 不包含这样的成员 - Identifier 'required' is not defined. '__type' does not contain such a member 未定义标识符“名称”。 “对象”不包含这样的成员 - Identifier 'name' is not defined. 'object' does not contain such a member 标识符“图像”未定义。 'never' 不包含这样的成员 - Identifier 'image' is not defined. 'never' does not contain such a member
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM