【发布时间】:2018-08-03 17:16:16
【问题描述】:
我有一个界面如下:
export interface testInterface
{
value1: string;
value2:string[];
SubValue:[{}]
}
我正在填充一个 testInterface 类型的数组——它绑定到一个下拉列表。一切正常。我希望下拉列表将“选择一个”作为其第一个对象。所以我做了以下事情:
default:testInterface[]=[] //==> since the bind array needs to be an array.
this.default.value1="----Please Select the Sub-Industry---";
this.default.value2=[];
this.default.SubValue=[];
我在 subValue 行收到此错误: 类型 'undefined[]' 不能分配给类型 '[{ value1: string;值2:字符串[];价值3...'。 类型“undefined[]”中缺少属性“0”。
为什么我不能说 dropdownArray[0].push=this.default???
有人能解释一下为什么吗?这个问题对你们中的一些人来说可能听起来很明显,但我是新手!
实际代码:
//how I fille the binded array:
subsDataofSelectedSector:ISubIndustry[]=[];
if (response.SubIndustries.length > 0) {
for (i = 0; i < response.SubIndustries.length; i++) {
this.subsDataofSelectedSector.push(response.SubIndustries[i]);
}
}
//the interface
export interface ISubIndustry
{
IndustrySector: string;
isSelected: string;
dataSubjectCategories:string[];
dataTypeCategories:string[];
SubIndustries:[{}]
}
//the default array
this.defaultSub.dataSubjectCategories =[];
this.defaultSub.dataTypeCategories=[];
this.defaultSub.IndustrySector="----Please Select the Sub-Industry---";
this.defaultSub.SubIndustries=[];
this.defaultSub.isSelected="true";
// i would like to to do the following and add the rest after index 0
this.subsDataofSelectedSector[0].push(this.defaultSub)
heer 是我必须注入数组的 JSON 文件的一部分。
{
"IndustrySector":"Other Services Activities",
"isSelected": "false",
"dataSubjectCategories":["DS.Employees","DS.Collaborators","DS.Legal Person","DS.Natural Person"],
"dataTypeCategories":["Personal Data"],
"SubIndustries": [
{
"IndustrySector":"Activities of Membership Organisations",
"isSelected": "false",
"dataSubjectCategories":[],
"dataTypeCategories":[],
"SubIndustries": [
{
"IndustrySector":"Activities of Other Membership Organisations",
"isSelected": "false",
"dataSubjectCategories":[],
"dataTypeCategories":[],
"SubIndustries":[
{
如你所见,一个可以有一些子值,每个子也可以有子。所以我将它定义为对象数组
【问题讨论】:
-
你能显示完整的组件代码吗?
-
您收到的错误表明 `SubValue:[{}]` 在
{}之间还有更多内容,您可以发布实际代码吗? -
我做了,请看一下
-
在你的界面中,你为什么不把你的
SubValue定义为一个数组而不是一个对象数组? -
您的代码还远不是演示错误的最小工作示例。我猜这个 `SubValue:[{}]` 可能应该是
SubValue:{}[],因为您可能想要一个数组而不是元组..但很难从你的帖子中说出来
标签: javascript angular typescript mean-stack