gpt4 book ai didi

javascript - 类型 'Object' 不可分配给 Angular 4.x 应用程序中的类型 'any[]'

转载 作者:行者123 更新时间:2023-11-30 20:08:34 25 4
gpt4 key购买 nike

我在 typescript 代码中收到以下错误,但不确定为什么有任何想法 - 它是 this.items 变量?

Type 'Object' is not assignable to type 'any[]'. The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?

export class GifpickerComponent implements OnInit {
public searchTerm : FormControl = new FormControl();
public items = [];

constructor(private gifpickerService: GifpickerService){
this.searchTerm.valueChanges
.subscribe(data => {
this.gifpickerService.search(data).subscribe(response =>{
console.log('subscribe', response);
this.items = response; /* line picked up in error */
})
})
}

/* 按要求提供服务 */

import { Injectable } from '@angular/core';
import { ApiService } from "../../services/api.service";
import { HttpClient, HttpParams, HttpResponse} from '@angular/common/http';

@Injectable()
export class GifpickerService {

constructor(
private http: HttpClient,
private apiService: ApiService
) {}

search(term){
console.log('term', term);
let url = this.apiService.getApiUrl('gif/search');
let params = new HttpParams()
.set('q', String(term))
.set('results_only', '1');

return this.http.get(url, {params: params})
.map(response => {
return response;
});
}
}

最佳答案

做这个改变

public items = {}

然后事情是,你正在将项目制作为 any 类型的数组,显然响应是 object 类型

关于javascript - 类型 'Object' 不可分配给 Angular 4.x 应用程序中的类型 'any[]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52625923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com