[英]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.movie
是Movie[]
类型,这意味着它是一个数组。
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.