Angular 我无法访问angluar中object的属性
我使用GetShipping方法通过id获取数据,该id返回一个对象,并在前端显示如下[object]。我想访问详细信息,然后在前端显示详细信息Angular 我无法访问angluar中object的属性,angular,Angular,我使用GetShipping方法通过id获取数据,该id返回一个对象,并在前端显示如下[object]。我想访问详细信息,然后在前端显示详细信息 export class ShipmentInfoComponent implements OnInit { shipment: any[]; constructor( private route: ActivatedRoute, private location: Location, private shipmentS
export class ShipmentInfoComponent implements OnInit {
shipment: any[];
constructor(
private route: ActivatedRoute,
private location: Location,
private shipmentService: ShipmentService
) {}
ngOnInit(): void {
this.getShipment();
}
getShipment(): void {
const id = this.route.snapshot.paramMap.get('id');
console.log(id);
this.shipmentService
.getShipment(id)
.subscribe((shipment) => (this.shipment = shipment));
}
}
我的GetShipping服务方法是
getShipment(id: string): Observable<any[]> {
const shipmentUrl = `${this.url}/${id}`;
return this.http.get<any[]>(shipmentUrl);
}
不能直接打印对象,必须打印任何要打印的值。如下图所示 如果您正在接收响应中的数组,则使用下面的
<div class="shipment" *ngFor="let ship of shipment">
Name: {{ ship.name }}
<br>
Mode: {{ship.mode}}
</div>
名称:{{ship.Name}
模式:{{ship.Mode}
否则,将服务方法返回类型数组更改为如下所示的对象
getShipment(id: string): Observable<any> {
const shipmentUrl = `${this.url}/${id}`;
return this.http.get<any>(shipmentUrl);
}
<div class="shipment" *ngIf="shipment">
Name: {{ shipment?.name }}
<br>
Mode: {{shipment?.mode}}
</div>
getshipping(id:string):可观察的您不能直接打印对象,您必须打印您想要打印的任何值。如下图所示
如果您正在接收响应中的数组,则使用下面的
<div class="shipment" *ngFor="let ship of shipment">
Name: {{ ship.name }}
<br>
Mode: {{ship.mode}}
</div>
名称:{{ship.Name}
模式:{{ship.Mode}
否则,将服务方法返回类型数组更改为如下所示的对象
getShipment(id: string): Observable<any> {
const shipmentUrl = `${this.url}/${id}`;
return this.http.get<any>(shipmentUrl);
}
<div class="shipment" *ngIf="shipment">
Name: {{ shipment?.name }}
<br>
Mode: {{shipment?.mode}}
</div>
getshipping(id:string):可观察的要调试您正在获取的信息,您可以向订阅中的匿名函数添加一个console.log(shipping)
。@TrevorKropp我添加了console数据的图像数据的类型是对象,而不是数组。尝试将类型声明为any
而不是any[]
。若要调试所获取的信息,可以向订阅中的匿名函数添加console.log(shipping)
。@TrevorKropp我已添加了console数据的图像数据的类型是对象,而不是数组。尝试将类型声明为any
,而不是any[]
。我已经提到我已经尝试过了,并且我得到了类似的错误“error TS2339:Property'name'在类型“any[]”上不存在”。idashby您正在接收响应中的数组,因此需要使用for循环,然后打印名称和所有内容。我已经提到,我已经尝试过了,但遇到了类似的错误“错误TS2339:类型“any[]”上不存在属性“name”。“idashby”您正在接收响应数组,因此需要使用for循环,然后打印名称和所有内容。