我在Angular中为数据模型创建新类。
我在新文件中创建了类并在构造函数
中初始化它们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文件中,我声明了新的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"
}]
}
]
答案 0 :(得分:1)
你必须调用构造函数,而不是将CreditPoliciesDetails数组转换为CreditPolicies数组
details :CreditPoliciesDetails[] = new Array <CreditPoliciesDetails>();
//files : CreditPolicies[] = new Array <CreditPolicies>(this.details);
files : CreditPolicies = new CreditPolicies(this.details);
答案 1 :(得分:0)
(this.details);类型的参数&#39; CreditPoliciesDetails []&#39;不能分配给&#39; CreditPolicies&#39;。
类型的参数它只是说,你有
this.details
CreditPoliciesDetails类型的arrray,您试图传递给构造函数。 在这种情况下,用作构造函数的数组需要CreditPolicies类型的参数。您正在传递CreditPoliciesDetails数组。
另外,我建议使用以下语法代替上一段代码。
export class CreditPoliciesDetails {
constructor(
private _fileDate="fileDate",
private _fileName="fileName",
private fileSize="fileSize";.....
) {}
}
它节省了时间并使代码更符合条件。