typescript 类型对象上不存在属性长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50351381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
property length does not exist on type Object
提问by Ignacio Ara
I have a services that get's a JSON from docker. I want to get the amount of containers.
我有一个从 docker 获取 JSON 的服务。我想获取容器的数量。
So I subscribe to my service:
所以我订阅了我的服务:
ngOnInit() {
this.docker.getContainers().subscribe(containers => {
console.log(containers.length);
});
}
I get the right answer on the console and yet I get:
我在控制台上得到了正确的答案,但我得到:
property length does not exist on type Object
类型对象上不存在属性长度
Am I doing something wrong?
难道我做错了什么?
export class DockerService {
private baseUrl:string = 'http://localhost:8080/api/docker';
private headers = new HttpHeaders({'Content-Type':'application/json'});
private options = new HttpHeaderResponse({headers:this.headers});
constructor(private _http:HttpClient) { }
getContainers() {
return this._http.get(this.baseUrl + "/containers", this.options);
}
getImages() {
return this._http.get(this.baseUrl + "/images", this.options);
}
}
回答by Ignacio Ara
You should use (containers: any[])when declaring the var:
你应该在声明 var 时使用(containers: any[]):
ngOnInit() {
this.docker.getContainers().subscribe((containers: any[]) => {
console.log(containers.length);
});
}
回答by Benedikt Schmidt
Basically angular does not know which type is returned by your http call, therefore it autocasts it into an object, which has no length
value. In order for it to work, you have to typecast it to what you are expecting it to be, in your example probably an array of Containers.
基本上 angular 不知道您的 http 调用返回的是哪种类型,因此它将它自动转换为一个没有length
价值的对象。为了使其工作,您必须将其类型转换为您期望的样子,在您的示例中可能是一组容器。
getContainers(): Container[] {
return this._http.get<Container[]>(this.baseUrl + "/containers", this.options);
}
This way angular will know what to expect and the attributes should exist. This of course depends on the Container
class, which you probably don't have. Cleanest way would be to create this class and define its attributes to what is coming back from the backend.
这样 angular 就会知道会发生什么并且属性应该存在。这当然取决于Container
您可能没有的课程。最简洁的方法是创建此类并将其属性定义为从后端返回的内容。
However if for some reason you don't want to do this, you can probably just tell angular that you're expecting an array of any
, which does not provide the type safety, but at least you'll have your length attribute
.
但是,如果由于某种原因您不想这样做,您可能只是告诉 angular 您期望的是一个 的数组any
,它不提供类型安全,但至少您将拥有length attribute
.
getContainers(): any[] {
return this._http.get<any[]>(this.baseUrl + "/containers", this.options);
}
回答by Azir Yasin
Since by default the HttpClient gets the response as an Object it does not have those properties. You can declare the containers as any[]and access the length property.
由于默认情况下 HttpClient 将响应作为对象获取,因此它没有这些属性。您可以将容器声明为any[]并访问 length 属性。
ngOnInit() {
this.docker.getContainers().subscribe(containers : any[] => {
console.log(containers.length);
});
}
So change the code as above and it would work
所以改变上面的代码,它会工作
回答by Daniel Segura Pérez
You should map your response, try this:
你应该映射你的响应,试试这个:
getContainers() {
return this._http.get(this.baseUrl + "/containers", this.options).map(res => {
return res.json();
});
}
getImages() {
return this._http.get(this.baseUrl + "/images", this.options).map(res => {
return res.json();
});
}
回答by MD Ashik
3/24/2019
2019/3/24
Angular-7
Angular-7
I get Solution by using... Observable<any[]>
我通过使用得到解决方案... Observable<any[]>
Example:
例子:
getContainers(): Observable<any[]> {
return this._http.get<any[]>(this.baseUrl + "/containers", this.options);
}