angular7 - 注入在构造函数中定义但在同一个类的函数中未定义的 BsModalService
问题描述
我在我自己的提供程序中注入 BsModalService,在构造函数中定义了 bs 模态服务,但在同一类的函数中却没有。
我尝试使用注入属性,尝试添加 deps 属性。
这里是我的提供者类的相关部分:
constructor(
private http: HttpClient,
private modalService: BsModalService
) {
// here the service is defined and everything is good
console.log(this.modalService);
}
public errorHandler(e?: any) {
// here the service is undefined
console.log(this.modalService);
if (this.modalService.getModalsCount() <= 1) {
this.modalService.show(NetworkErrorDialogComponent, {initialState: e});
}
return of(e);
}
public getData(options?: HttpOptions) {
return this.http.get('path/to/my/api', options).pipe(
map(response => response.data || response),
catchError(this.errorHandler)
);
}
解决方案
解决了它,我将 errorHandler 转移到 catchError 而不是调用它。这是解决方案:
catchError(e => this.errorHandler(e))
推荐阅读
- sed - 正斜杠的 sed 命令帮助
- javascript - 用下划线循环键值对象
- http - haproxy 重写 uri 保留后端标头主机
- visual-studio - 为什么我的 MDAC 2.7 参考在 Windows 10 上不起作用?
- xml - XSLT 2.0 转换的文本输出中的空格过多
- javascript - 使用 async\await 重写基于 Promise 的代码
- java - JExcel - 在关闭 WritableWorkbook 之前修改多个单元格
- javascript - Mongoose + Nodejs 验证中此变量的行为
- regex - 正则表达式在数字旁边拿起“-”,但不在字母旁边
- r - 使用对 R 中列名的变量引用来提取数据子集