Angular 与RxJS成角度:确保服务';使用http.get的构造函数在调用方法之前完成

Angular 与RxJS成角度:确保服务';使用http.get的构造函数在调用方法之前完成,angular,rxjs,angular6,Angular,Rxjs,Angular6,在Angular 6中,我有一个ConfigService,它要求构造函数必须完成才能正常工作: import{HttpClient,HttpHeaders,HttpErrorResponse}来自“@angular/common/http”; 从“@angular/core”导入{Injectable}”; 从“rxjs”导入{可观察、可订阅、订阅}; @可注射() 导出类配置服务{ 公钥:任意; 构造函数(专用http:HttpClient){ this.http.get('config/c

在Angular 6中,我有一个
ConfigService
,它要求构造函数必须完成才能正常工作:

import{HttpClient,HttpHeaders,HttpErrorResponse}来自“@angular/common/http”;
从“@angular/core”导入{Injectable}”;
从“rxjs”导入{可观察、可订阅、订阅};
@可注射()
导出类配置服务{
公钥:任意;
构造函数(专用http:HttpClient){
this.http.get('config/config.json').subscribe(
数据=>{
this.configKeys=数据;
},
(错误:HttpErrorResponse)=>{
console.log(错误消息);
}
);
}
getDocBaseRoute():字符串{
返回this.configKeys.docBaseRoute;
}
getAuthenticationBaseRoute():订阅{
返回this.configKeys.authenticationBaseRoute;
}
}
当我尝试调用服务的方法时,会触发一个问题:

this.baseUrl=this.configservice.getAuthenticationBaseRoute();
实际上,此时
configKeys
未定义,因此
返回this.configKeys.authenticationBaseRoute抛出一个错误


所以,你能帮我确定在调用服务的方法之前,构造函数已经完成了吗?

你没有办法这样做,这将违反RxJS的概念

在我看来,这更多的是一个概念问题,你可以有不同的解决方案:

  • configKeys
    定义默认值,以便
    getAuthenticationBaseRoute
    永不失败

  • 处理
    configKeys
    getAuthenticationBaseRoute
    中为
    null
    的情况,并使其返回
    未定义的
    。但您还必须处理调用
    getAuthenticationBaseRoute
    的情况

  • ConfigService
    中定义
    configKeys
    之前,使调用
    getAuthenticationBaseRoute
    的组件/服务/任何东西都不能调用它


  • 你没有办法做到这一点,这将违反RxJS的概念

    在我看来,这更多的是一个概念问题,你可以有不同的解决方案:

  • configKeys
    定义默认值,以便
    getAuthenticationBaseRoute
    永不失败

  • 处理
    configKeys
    getAuthenticationBaseRoute
    中为
    null
    的情况,并使其返回
    未定义的
    。但您还必须处理调用
    getAuthenticationBaseRoute
    的情况

  • ConfigService
    中定义
    configKeys
    之前,使调用
    getAuthenticationBaseRoute
    的组件/服务/任何东西都不能调用它


  • 好的,我找到了一个方法: 使用toPromise()将可观察转化为承诺

    import{HttpClient,HttpHeaders,HttpErrorResponse}来自“@angular/common/http”;
    从“@angular/core”导入{Injectable}”;
    @可注射()
    导出类配置服务{
    公钥:任意;
    构造函数(专用http:HttpClient){
    }
    异步getGestionDocumentBaseRoute():承诺{
    等待这个。初始化();
    返回this.configKeys.gestionDocumentBaseRoute;
    }
    私有异步初始化():承诺{
    如果(!this.configKeys){
    等待这个.http.get('config/config.json')。toPromise()
    .那么(
    数据=>{
    this.configKeys=数据;
    }
    )
    .接住(
    (错误:HttpErrorResponse)=>{
    控制台日志(错误消息);
    返回false;
    }
    );
    }
    返回true;
    }
    异步getAuthenticationBaseRoute():承诺{
    等待这个。初始化();
    返回this.configKeys.authenticationBaseRoute;
    }
    }
    
    好的,我找到了一种方法: 使用toPromise()将可观察转化为承诺

    import{HttpClient,HttpHeaders,HttpErrorResponse}来自“@angular/common/http”;
    从“@angular/core”导入{Injectable}”;
    @可注射()
    导出类配置服务{
    公钥:任意;
    构造函数(专用http:HttpClient){
    }
    异步getGestionDocumentBaseRoute():承诺{
    等待这个。初始化();
    返回this.configKeys.gestionDocumentBaseRoute;
    }
    私有异步初始化():承诺{
    如果(!this.configKeys){
    等待这个.http.get('config/config.json')。toPromise()
    .那么(
    数据=>{
    this.configKeys=数据;
    }
    )
    .接住(
    (错误:HttpErrorResponse)=>{
    控制台日志(错误消息);
    返回false;
    }
    );
    }
    返回true;
    }
    异步getAuthenticationBaseRoute():承诺{
    等待这个。初始化();
    返回this.configKeys.authenticationBaseRoute;
    }
    }
    
    您应该在整个过程中使用RxJS,这样才能正常工作

    从“@angular/common/http”导入{HttpClient,HttpErrorResponse};
    从“@angular/core”导入{Injectable}”;
    从“rxjs”导入{observeable};
    从“rxjs/operators”导入{shareReplay,map};
    @可注射()
    导出类配置服务{
    私有配置:可观察;
    构造函数(私有http:HttpClient){}
    getDocBaseRoute():可观察{
    返回这个.getConfiguration().pipe(map(data=>data.docBaseRoute));
    }
    getAuthenticationBaseRoute():可观察{
    返回这个.getConfiguration().pipe(map(data=>data.authenticationBaseRoute));
    }
    私有getConfiguration():
    
    import { HttpClient, HttpHeaders, HttpErrorResponse } from "@angular/common/http";
    import { Injectable } from "@angular/core";
    
    @Injectable()
    export class ConfigService {
        public configKeys: any;
    
        constructor(private http: HttpClient) {
        }
    
        async getGestionDocumentBaseRoute(): Promise<string> {
            await this.initialize();
            return this.configKeys.gestionDocumentBaseRoute;
        }
    
        private async initialize(): Promise<boolean> {
            if (!this.configKeys) {
                await this.http.get('config/config.json').toPromise()
                    .then(
                        data => {
                            this.configKeys = data;
                        }
                    )
                    .catch(
                        (err: HttpErrorResponse) => {
                            console.log(err.message);
                            return false;
                        }
                    );
            }
            return true;
        }
    
        async getAuthenticationBaseRoute(): Promise<string> {
            await this.initialize();
            return this.configKeys.authenticationBaseRoute;
        }
    }