Javascript &引用;TypeError:这是未定义的;在访问私有metod时
在一个离子组件中,我使用了惊人的Javascript &引用;TypeError:这是未定义的;在访问私有metod时,javascript,angular,typescript,ionic4,Javascript,Angular,Typescript,Ionic4,在一个离子组件中,我使用了惊人的标签。使用header函数可以拆分列表中的项目。现在,分离这段代码,我将它的一部分提取到私有函数sameGroup。虽然它返回TypeError:当我想从myHeaderFn函数调用它时,它是未定义的。为什么? (是的,我知道,我可以轻松地将该功能代码插入myHeaderFn。但这不是问题所在。) ionic组件调用myHeaderFn <ion-virtual-scroll [items]="translations" [headerFn]="myHea
标签。使用header函数可以拆分列表中的项目。现在,分离这段代码,我将它的一部分提取到私有函数sameGroup
。虽然它返回TypeError:当我想从myHeaderFn
函数调用它时,它是未定义的。为什么?
(是的,我知道,我可以轻松地将该功能代码插入myHeaderFn
。但这不是问题所在。)
ionic组件调用myHeaderFn
<ion-virtual-scroll [items]="translations" [headerFn]="myHeaderFn">
<ion-item-divider *virtualHeader="let header">
{{ header }}
</ion-item-divider>
<!-- need to wrap this into the div, so that *virtualItem is not part of the component -->
<div *virtualItem="let trans">
<!-- translation (not translations) referes to the model of the component -->
<app-transitem [translation]=trans></app-transitem>
</div>
</ion-virtual-scroll>
{{header}}
您需要使用箭头功能
myHeaderFn = (record, recordIndex, records) => {
if (this.sameGroup(recordIndex, records)) {
return null;
} else {
return record['term'].substring(0, 1);
}
}
箭头函数没有自己的此值。arrow函数中的该值始终从封闭范围继承
如何称呼myHeaderFn
?您没有显示它。@AJT82它是由ion虚拟滚动触发的。您可以调用sameGroup
,即使它没有标记为private??Thans。但为什么呢?它背后的解剖结构是什么?@李玄。检查这个stackblitz示例@有什么不寻常的事吗?
myHeaderFn = (record, recordIndex, records) => {
if (this.sameGroup(recordIndex, records)) {
return null;
} else {
return record['term'].substring(0, 1);
}
}