【发布时间】:2019-10-27 10:23:56
【问题描述】:
这是我尝试使用承诺的打字稿功能:
public onEditSubmit() {
const model = this._sharedService.createUpdateModel(
null,
this.editForm
) as LOG;
model.fileId = this.fileId;
model.startEffectiveDate = Shared.toISODate(model.startEffectiveDate);
model.endEffectiveDate = Shared.toISODate(model.endEffectiveDate);
let deferredExecutionCheck = new Promise((resolve, reject) => {
this._updateService
.getAllById(this.selectedItem.LogId)
.subscribe(
r => {
this.records = r;
this.records.forEach(element => {
if (
element.StatusId === 1 ||
element.StatusId === 2 ||
element.StatusId === 4 ||
element.StatusId === 5
) {
this._notificationService.showErrorMessage(
`MESSAGE GOES HERE`,
"IN PROGRESS"
);
reject("In Progress");
}
});
resolve("Not In Progress");
},
e => {
throw e;
}
);
console.log("finished");
});
let originalEditSubmit = function(result: any) {
if (this.editMode === "Add") {
this.add(model);
} else {
if (
(model.wfStatusId === Status.Review ||
model.wfStatusId === Status.LoadFailed ||
model.wfStatusId === Status.Completed) &&
model.eventStatusId === eStatus.Cancelled
) {
this._confirmDlg.closable = false;
this._confSvc.confirm({
accept: () => {
model.cancelRcdb = true;
this.update(model);
},
message: "Cancel RCdB Dataset?",
reject: () => {
model.cancelRcdb = false;
this.update(model);
}
});
} else {
this.update(model);
}
}
};
deferredExecutionCheck.then(
result => originalEditSubmit(result),
error => console.log("error", error)
);
}
错误:未捕获(承诺中):TypeError:无法读取属性 未定义类型错误的“editMode”:无法读取属性“editMode”的 undefined at originalEditSubmit
我将 this.fileId 属性移到 originalEditSumbmit 方法之外,现在正在读取它。但现在看来this.editMode 也遇到了同样的问题。
我不能像这样在我的 Promise 中包含这些属性吗?
【问题讨论】:
-
改用箭头函数。
-
@str - 是不是因为这个答案(在被注释的代码中)://在箭头函数中,'this'指针是按词法解释的,如果是这样,什么是“词法”什么意思?
标签: javascript angular typescript