数组未分配给通用对象

时间:2018-03-10 06:48:37

标签: typescript angular5

这是我的代码

structure1

export class BankingChannePageData<T> {
    pagedata: T;
    revnum: number;
    pagetype: string;
}

structur2

import { BankingChannelCommonRevenueCenter } from '../../models/banking-channel/banking-channel-common-revenue-center';
export class BankingChannelRevenueCenter<T> {
    common: BankingChannelCommonRevenueCenter;
    specific: T;
}

Strurture 3

export class BankingChannelCommonRevenueCenter {
    stateid: number;
    statename: string;
    districtid: number;
    districtname: string;
    subdistrictid: number;
    subdistrictname: string;
    centerid: number;
    centername: string;
    bankid: number;
}

这里我创建了一个数组

bcrevenuecenterarray: BankingChannelCommonRevenueCenter[] = [];

并将创建的数组分配给通用对象 像这样

const bcrevenuecenterrequest = new BankingChannePageData<BankingChannelRevenueCenter<any>>();
    bcrevenuecenterrequest.pagedata = new BankingChannelRevenueCenter<any>();
    bcrevenuecenterrequest.pagedata.common = new BankingChannelCommonRevenueCenter();
    bcrevenuecenterrequest.pagedata.common  = this.bcrevenuecenterarray;

问题 键入&#39; BankingChannelCommonRevenueCenter []&#39;不能分配给类型&#39; BankingChannelCommonRevenueCenter&#39;。   财产&#39; stateid&#39;在&#39; BankingChannelCommonRevenueCenter []&#39;。

中缺少

1 个答案:

答案 0 :(得分:1)

您已将该字段声明为BankingChannelCommonRevenueCenter对象的数组,但之后您尝试将该值设置为单个BankingChannelCommonRevenueCenter。

BankingChannelCommonRevenueCenter []表示“数组中每个项目都是BankingChannelCommonRevenueCenter实例的数组”。

如果您希望该字段仅包含一个项目,则将其类型声明为 BankingChannelCommonRevenueCenter代替BankingChannelCommonRevenueCenter [](删除方括号)。