首页 > 解决方案 > 注入在构造函数中定义但在同一个类的函数中未定义的 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)
);
            }

标签: angular7

解决方案


解决了它,我将 errorHandler 转移到 catchError 而不是调用它。这是解决方案:

catchError(e => this.errorHandler(e))

推荐阅读