【发布时间】:2020-05-04 13:30:24
【问题描述】:
为什么未定义接口属性“title”?即使我正在尝试为它分配一个值。
这里是代码。这是我试图检查是否正在设置标题,但它不是......
export class SidebarComponent implements OnInit {
public theme = KTheme;
public testModels: any[];
secondModel: IKSecondColumnFeedList = {title: 'whatTitle'};
constructor() {
this.secondModel = { title: 'What?'};
const kIconTest: KIcon = {icon: 'bell'};
const kFeedFooter: IKFeedListFooter = {isTimeAgo: false, plainText: 'plain text'};
const thirdModel: IKThirdColumnFeedList = {value: 20, actionClickTrigger: false, status:
'danger', tooltip: false, kIcon: kIconTest };
this.secondModel = {actionClickTrigger: false, tooltip: false, footer: kFeedFooter, description:
'description'} as IKSecondColumnFeedList;
const test: IKFeed = { id: 'noriId', thirdColumn: thirdModel, secondColumn: this.secondModel };
this.testModels = [
{ test }
];
}
ngOnInit() {
this.secondModel = { title: 'What?'};
}
}
还有接口(模型)。
export interface IKThirdColumnFeedList {
value: number;
status?: string;
kIcon?: KIcon;
tooltip?: boolean;
actionClickTrigger?: boolean;
}
这是根对象。
export interface IKFeed {
id: string;
secondColumn?: IKSecondColumnFeedList;
thirdColumn?: IKThirdColumnFeedList;
}
更新
export class SidebarComponent implements OnInit {
public theme = KTheme;
public testModels: any[];
constructor() {
const kIconTest: KIcon = {icon: 'bell'};
const kFeedFooter: IKFeedListFooter = {isTimeAgo: false, plainText: 'plain text'};
const thirdModel: IKThirdColumnFeedList = {value: 20, actionClickTrigger: false, status: 'danger', tooltip: false, kIcon: kIconTest };
const secondModel = {title: 'someTitle', actionClickTrigger: false, tooltip: false, footer: kFeedFooter, description: 'description'} as IKSecondColumnFeedList;
const test: IKFeed = { id: 'noriId', thirdColumn: thirdModel, secondColumn: secondModel };
this.testModels = [
{ test }
];
}
ngOnInit() {
}
}
上面的代码在侧边栏组件中,我将列表传递给另一个组件,然后我试图显示来自 ngFor 的值,它说它们仍然是未定义的。我做错了什么?
如果我将控制台记录数组,它将显示里面的所有数据,但由于某种原因,当我想在 HTML 中显示它时会引发错误。
【问题讨论】:
标签: angular interface undefined