问题描述
有没有一种方法可以在不使用此关键字的情况下将参数从订阅块或错误块传递到“finally”块。我在下面的尝试不起作用
this.service.create()
.pipe(first())
.subscribe (
(resp) => {
let argumentToFinally = '';
if (!!resp.taskInfo) {
argumentToFinally = this.showError(resp.taskInfo);
} else {
close();
// if else block,'argumentToFinally' is still ''
}
},(error) => {
this.isCallingApi = false;
argumentToFinally = this.showError('create',undefined,error);
},(argumentToFinally) => {
close(argumentToFinally)
});
close(argumentOfFinally?: any) {
if (!!argumentOfFinally) {
..
}
this.closeWizard();
}
解决方法
- 您只能在
finally
块中使用全局变量 (this.variableName),因为finally
块不接受任何参数。 - 在
finally
块之后不会调用error
块,因为发生错误时订阅被取消。
欲了解更多信息,请检查 -> Using observables to pass values