我正在尝试使用构造函数为类属性(即object)提供默认参数。 。 。其中是对象数组。我没有尝试的任何方法。 。 。
最后我要让rowData
对象的authData
属性是一个只有一个条目(对象)的数组。 。 。即authData
:
{"catText": {"catName": "User Data", . . . },
"rowData": [{
{"userName":'', "style":{ . . .},
. . .
{"uID": "User ID", "style"{: . . . "width": 3}
}]
}
我尝试过的一种方法是使用Object.values().map(obj => [obj])
,但这是行不通的。 (简化的)代码(不会编译... 'A parameter property is only allowed in a constructor implementation . . . A parameter initializer is only allowed in a function or constructor implementation' . . .
)如下:
export interface AuthBlock {
"catText": {"catName": string}
"rowData": {
"userName": {"Username": string,"style": {"type": string, "width": number}},
"password":{"Password": string,"style": {"type": string, "width": number}},
"uId":{"User ID": string,"style": {"type": string, "width": number}}
}[]
}
export class AuthRow {
constructor(
public authData: AuthBlock = {
"catText": {"catName": "User Data"},
"rowData": Object.values({{
"userName": {"Username": '',"style": {"type": "text", "width": 3}},
"password":{"Password": '',"style": {"type": "password", "width": 5}},
"uId":{"User ID": '',"style": {"type": "text", "width": 3}}
}}).map(obj => [obj])
}
){}
}
如果有什么想法,请多谢!
答案 0 :(得分:1)
Object.keys(obj.rowData)
.map(key => ({ [key]: obj.rowData[key] }))
获取对象上的键,使用map将其转换为数组,然后将其转换为单独的对象。
编辑:
export interface AuthBlock {
"catText": {"catName": string}
"rowData": {
"userName": {"Username": string,"style": {"type": string, "width": number}},
"password":{"Password": string,"style": {"type": string, "width": number}},
"uId":{"User ID": string,"style": {"type": string, "width": number}}
}[]
}
export class AuthRow {
constructor(
public authData: AuthBlock = {
"catText": {"catName": "User Data"},
// Open array here
"rowData": [{
"userName": {"Username": '',"style": {"type": "text", "width": 3}},
"password":{"Password": '',"style": {"type": "password", "width": 5}},
"uId":{"User ID": '',"style": {"type": "text", "width": 3}}
}]
// Close here
}
){}
}