在我的Angular应用程序中,我有一个组件:
import { MakeService } from './../../services/make.service';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-vehicle-form',
templateUrl: './vehicle-form.component.html',
styleUrls: ['./vehicle-form.component.css']
})
export class VehicleFormComponent implements OnInit {
makes: any[];
vehicle = {};
constructor(private makeService: MakeService) { }
ngOnInit() {
this.makeService.getMakes().subscribe(makes => { this.makes = makes
console.log("MAKES", this.makes);
});
}
onMakeChange(){
console.log("VEHICLE", this.vehicle);
}
}
Run Code Online (Sandbox Code Playgroud)
但在"制造"属性中我有一个错误.我不知道该怎么做...
我有产品界面:
export interface Product{
code: string;
description: string;
type: string;
}
Run Code Online (Sandbox Code Playgroud)
使用方法调用产品端点的服务:
public getProducts(): Observable<Product> {
return this.http.get<Product>(`api/products/v1/`);
}
Run Code Online (Sandbox Code Playgroud)
我使用此服务获取产品的组件。
export class ShopComponent implements OnInit {
public productsArray: Product[];
ngOnInit() {
this.productService.getProducts().subscribe(res => {
this.productsArray = res;
});
}
}
Run Code Online (Sandbox Code Playgroud)
在这种状态下,我得到了错误:
[ts]类型“产品”缺少类型“产品[]”中的以下属性:长度,弹出,推入,连续和另外26个。[2740]
删除productsArray变量上的类型可消除该错误,但不知道为什么此方法不起作用,因为服务器响应是类型为Products?的对象数组。