我正在尝试使用构造函数为class属性提供默认参数,它是一个对象。。。其内部是一个对象数组。我试过的都不管用。。。
我想要结束的是让authData
对象的rowData
属性成为一个只有一个条目的数组(一个对象)。。。即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])
}
){}
}
如果您有任何想法,请提前表示感谢!
发布于 2019-04-01 02:22:20
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
}
){}
}
https://stackoverflow.com/questions/55443879
复制相似问题