我有一个打字稿类,其中包含以下内容
getOrgList(oo: fhir.Organization) {
var olist: orgRoles[] = [];
var filtered = oo.extension.filter(this.getRoleExt);
filtered.forEach(function (value) {
var org = new orgRoles();
value.extension.forEach(function (innerValue) {
switch (innerValue.url) {
case 'role':
org.roleName = innerValue.valueCoding.display;
break;
case 'primaryRole':
org.primaryRole = innerValue.valueBoolean;
break;
case 'activePeriod':
var periodType = innerValue.valuePeriod.extension[0].valueString;
var periodExt = innerValue.valuePeriod;
var periodDisplay= this.getPeriodDisplay(periodExt);
break;
case 'status':
org.activeStatus = innerValue.valueString;
break;
}
});
olist.push(org);
});
return olist;
}
当前在线失败
var periodDisplay= this.getPeriodDisplay(periodExt);
有错误
ERROR TypeError: Cannot read property 'getPeriodDisplay' of undefined
但是,将getPeriodDisplay定义为同一类中的下一个方法。
getPeriodDisplay(pp) {
return "++++";
}
我还有其他代码从类外部调用getPeriodDisplay(),但是从类中的其他方法调用它似乎会导致问题
关于我该如何解决的任何想法?
答案 0 :(得分:0)
您需要将常规功能切换为箭头功能才能访问正确的this
(类实例):
getOrgList(oo: fhir.Organization) {
var olist: orgRoles[] = [];
var filtered = oo.extension.filter(this.getRoleExt);
filtered.forEach((value) => {
var org = new orgRoles();
value.extension.forEach((innerValue) => {
switch (innerValue.url) {
case 'role':
org.roleName = innerValue.valueCoding.display;
break;
case 'primaryRole':
org.primaryRole = innerValue.valueBoolean;
break;
case 'activePeriod':
var periodType = innerValue.valuePeriod.extension[0].valueString;
var periodExt = innerValue.valuePeriod;
var periodDisplay= this.getPeriodDisplay(periodExt);
break;
case 'status':
org.activeStatus = innerValue.valueString;
break;
}
});
olist.push(org);
});
return olist;
}