angular - 如何将参数传递给不带括号的函数?
问题描述
我有以下代码:
this.somePromiseFn<T> // this fn is actually a promise
.then(res => res as T)
.catch(err => this.handleError<T>(err));
handleError<T>(err: HttpErrorResponse) {
// do something here
// and return like
return {} as T;
}
上面的代码工作正常。但是如何在不使用另一个回调函数的情况下将err
参数直接传递给方法。handleError
由于catch
默认情况下将 传递err
给回调函数,它也应该将 传递err
给handleCatchError
. 我想要类似的东西:
this.somePromiseFn<T>
.then(res => res as T)
.catch(this.handleCatchError<T>); // this line gives error 'An argument for 'err' was not provided'
但是上面的代码会产生错误说:
预期 1 个参数,但得到 0。
未提供“err”参数。
虽然我已经访问过以下问题:
但上述问题也建议使用另一个回调函数。
解决方案
this.somePromiseFn<T>
.then(res => res as T)
.catch(this.handleCatchError<T>);
这应该可以,尝试重现错误,但可以查看演示,可能是我遗漏了一些东西,如有必要,请检查并更新。
推荐阅读
- angular - 在离子中,为什么标签无法将值发送到另一个页面?
- java - Java中的构造函数链和可见性
- doxygen - 与 . 在文件名中
- python - 网络中的python,如果多个数据包同时到达,socket做什么
- android - 为什么我的应用没有填满整个屏幕?
- reactjs - 如何为所有 Material-UI 组件添加填充和边距?
- c++ - C++ 外部定义内联函数
- python - autopep8 不修复任何缩进
- firebase - Firebase 云功能 POST HTTPS 来自 ESP8266 的请求
- android - 由于缺少权限,无法加载 Android Facebook 登录