[英]declaring an array type of custom object to a typescript function
I hae a model of car imported into current component. 我有一个导入到当前组件中的汽车模型。 Trying to declare an array of cars as a return type to one of the functions.
试图将一组汽车声明为函数之一的返回类型。 But the compiler would not recognize the car array type.
但是编译器无法识别汽车阵列类型。 Can you please help ?
你能帮忙吗?
carcomponent.ts carcomponent.ts
import { Car } from '../models/car';
export class carComponent implements onInit {
public car : Car;
carsavailable(): Car[] { //delcaring Car[] is not recognized.
}
}
car.ts ....... car.ts .......
export class Car {
name: string;
}
ERROR 错误
The compiler underlines Car[] in red and says 'A function whose delcared type is neither 'void' nor 'any' must return a value. 编译器用红色下划线标出Car [],并说“代入类型既不是“ void”也不是“ any”的函数必须返回一个值。
Do I have to plug in car somewhere or change return type declaration way. 我是否必须在某处插入汽车或更改返回类型声明方式。
As you are not returning any value from the function so that compiler is showing the error 由于您没有从函数返回任何值,因此编译器显示了错误
carsavailable(): Car[] { //delcaring Car[] is not recognized.
return [];
}
You are declaring a method with a return type so it should be returning a value 您正在声明具有返回类型的方法,因此它应该返回一个值
carsavailable(): Car[] {
return [];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.