我正在为数据模型创建新的角度类。
我在新文件中创建了类并在构造函数中初始化它们
export class CreditPoliciesDetails {
private _fileName : string ="" ;
private _fileDate : string ="" ;
private _fileSize : string ="" ;
private _path : string="";
private _extention: string="";
constructor() {
this._fileDate="fileDate";
this._fileName="fileName";
this._fileSize="fileSize";
this._path="path";
this._extention="extension";
}
}
export class CreditPolicies {
private _document : CreditPoliciesDetails[];
private _directory: string
constructor (details: CreditPoliciesDetails[]) {
this._document = details;
this._directory="";
}
get category() {
return this._document
}
public details (detail) {
console.log(detail)
this._document= detail;
}
}
然后在组件ts文件中声明new
CreditPoliciesDetails
数组,然后将其传递给files
数组,这是CreditPolicies
类型的数组。 details :CreditPoliciesDetails[] = new Array <CreditPoliciesDetails>();
files : CreditPolicies[] = new Array <CreditPolicies>(this.details);
在vs代码中,当我将
new Array <CreditPolicies>(this.details);
Argument of type 'CreditPoliciesDetails[]' is not assignable to parameter of type 'CreditPolicies'. Property '_document' is missing in type 'CreditPoliciesDetails[]'.
当我console.log
files
和details
时,我得到空数组。我在等这个格式
[
{
"directory":"string",
"document":[{
"extension":"string",
"lastModifiedDate":"string",
"name":"string",
"path":"string",
"size":"0"
}]
}
]
最佳答案
必须调用构造函数,而不是将creditpoliciesdetails数组强制转换为creditpolicies数组
details :CreditPoliciesDetails[] = new Array <CreditPoliciesDetails>();
//files : CreditPolicies[] = new Array <CreditPolicies>(this.details);
files : CreditPolicies = new CreditPolicies(this.details);
关于javascript - Typescript类不接受参数类型,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49917139/