【发布时间】:2022-01-13 17:13:34
【问题描述】:
Angular 抛出以下错误:
“类型 'Object' 缺少类型 'never[]' 中的以下属性:length、pop、push、concat 和另外 28 个。
this.movies = 数据;"
// movie-list.component.ts
// ...
export class MovieListComponent implements OnInit {
movies = [];
constructor(
private apiService: ApiService) { }
ngOnInit() {
this.apiService.getMovies().subscribe(
data => {
this.movies = data;
})}
}
我已经更改了“电影”变量
movies: any[] = [];
但错误仍然出现
// api.service.ts
// ...
export class ApiService {
baseUrl = 'http://127.0.0.1:8000/api/movies/'
headers = new HttpHeaders({
'Content-Type': 'application/json',
Authorization: 'token 61a1e3bfcfaea4fceee08d52fa132c788204d5e4'
})
constructor(
private http: HttpClient
) {}
getMovies() {
return this.http.get(this.baseUrl, {headers: this.headers});
}
}
console.log(数据)
(3) [{…}, {…}, {…}]
0: {id: 1, title: 'Titanic', description: 'Romantic movie '}
1: {id: 2, title: 'Avatar', description: 'SiFy movie'}
2: {id: 4, title: 'Dune', description: 'SiFy movie'}
length: 3
[[Prototype]]: Array(0)
【问题讨论】:
-
能否也显示api.service.ts中的代码?
-
好的,在这种情况下,看看
getMovies()的实现会有所帮助。 -
您是否编辑过问题?我没有看到任何新东西。
-
好的,现在完成,代码已编辑。
-
目前还不清楚
getMovies返回什么类型(get没有任何类型规范),但它似乎很可能返回一个对象的Observable> 某种,你期待一个数组。也许您想要的数组是该对象内的某个属性。在this.movies = data;处设置调试断点或执行console.log,看看data究竟是什么。
标签: angular13