首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在APP_INITIALIZER上的app之后加载APP_INITIALIZER文件

在APP_INITIALIZER上的app之后加载APP_INITIALIZER文件
EN

Stack Overflow用户
提问于 2020-02-07 09:06:49
回答 2查看 2.6K关注 0票数 2

我一直试图在DevOps上部署一个Angular 8应用程序,并在.json文件中使用配置,以避免为不同的环境重新构建整个应用程序。

我使用这两个帖子来创建所有配置:

使用Azure DevOps不断将角部署到Azure应用程序服务

堆栈溢出回答:

App.settings -角度方向?

Note表示,我对使用environment.ts方法不感兴趣,因为这种方式将要求我为每个环境重新构建解决方案。

所以,我准备了所有的代码如下:

代码语言:javascript
复制
@NgModule({
    declarations: [
        AppComponent
    ],
    imports: [
        BrowserModule
    ],
    providers: [
        {
             provide: APP_INITIALIZER,
             useFactory: (appConfigService: ConfigService) => {
             return () => {
                //Make sure to return a promise!
                return appConfigService.loadAppConfig();
             };
          },
          deps: [ConfigService],
          multi: true
       }
    ],
    bootstrap: [AppComponent]
 })
 export class AppModule {}

我的ConfigService.ts

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class ConfigService {
private appConfig: any;

constructor(private http: HttpClient) {}

loadAppConfig() {
  return this.http.get('./assets/appconfig.json')
    .toPromise()
    .then(config => {
      this.appConfig = config;
    });
}

get apiBaseUrl() {
    if (!this.appConfig) {
      throw Error('Config file not loaded!');
    }

    return this.appConfig.apiUrl;
  }
}

然后,需要加载appconfig.json信息的主要对象:

代码语言:javascript
复制
  export class ApiService {
  apiUrl: string;

     constructor(private readonly httpClient: HttpClient,
          private configService: ConfigService) { 
            this.apiUrl = this.configService.apiBaseUrl;
     }

     ngOnInit() {
       this.apiUrl = this.configService.apiBaseUrl;
     }    
  }

但是,在加载应用程序时,出现了以下消息:

如果我调试应用程序,appsettings.json文件正在加载信息,但是它看起来就像是在加载应用程序设置之前发生的角度init。

我做错什么了?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-02-07 15:21:57

因此,除了一件重要的事情之外,一切都配置得很好:构造函数只用于注入依赖项。因此,在这一点上,我不能这样说:

代码语言:javascript
复制
constructor(private readonly httpClient: HttpClient,
      private configService: ConfigService) { 
        this.apiUrl = this.configService.apiBaseUrl;
 }

溶液

然后,我删除了构造函数中的行:

代码语言:javascript
复制
constructor(private readonly httpClient: HttpClient,
      private configService: ConfigService) { 
 }

只要我需要它,就给apiBaseUrl打电话,它就起作用了:

代码语言:javascript
复制
public get<T>(url: string): Observable<T> {
    return this.httpClient.get<T>(`${this.configService.apiBaseUrl}${url}`);
}
票数 0
EN

Stack Overflow用户

发布于 2020-02-07 09:14:21

您可以返回一个Promise并在HTTP请求的subscribe回调中解析它,如下所示:

代码语言:javascript
复制
loadAppConfig() {
  return new Promise((resolve) => {
     this.http.get('./assets/appconfig.json').subscribe(config => {
        this.appConfig = config;
        resolve();
     })
  });
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60110257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档