Created
December 20, 2019 04:31
-
-
Save ZhenDeng/36c03a144800cd02185a23d93715d8b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. in module provider | |
{ provide: APP_CONFIG, useValue: AppConfig } | |
2. app.config.ts | |
import { InjectionToken } from "@angular/core"; | |
import { IAuthConfig } from "../service/Authentication/auth.config"; | |
import { environment } from 'src/environments/environment'; | |
export let APP_CONFIG = new InjectionToken("app.config"); | |
export interface IAppConfig { | |
dateFormat: string; | |
dateFormatMomentJs: string; | |
environment: string; | |
apiEndpoint: string; | |
appAgencyWebUrl: string; | |
appFinanceWebUrl: string; | |
appCssFullUrl: string; | |
appFeedbackMailTo: string; | |
authConfig: IAuthConfig; | |
applicationVersionInfo: any; | |
userInfo: any; | |
validatorMetaData: any; | |
uiSettings: any; | |
} | |
export let AppConfig: IAppConfig = { | |
dateFormat: "dd MMM yyyy", | |
dateFormatMomentJs: "DD MMM YYYY", | |
environment: "", | |
apiEndpoint: environment.apiEndpoint, | |
appAgencyWebUrl: "", | |
appFinanceWebUrl: '', | |
appCssFullUrl: "", | |
appFeedbackMailTo: "", | |
authConfig: { applicationName: null, authEndpoint: null, contactAdmin: null, staffPortal: null, storedTokenName: 'admissionsToken' }, | |
validatorMetaData: { | |
}, | |
applicationVersionInfo: {}, | |
userInfo: {}, | |
uiSettings: { | |
} | |
}; | |
3. use in component | |
in constructor | |
@Inject(APP_CONFIG) private config: IAppConfig | |
I can use value in config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. in module provider | |
{ provide: APP_INITIALIZER, useFactory: (config: ServerConfigInitializer) => () => config.load(), deps: [ServerConfigInitializer], multi: true }, | |
2. server.config.initializer.ts | |
import { Inject, Injectable } from '@angular/core'; | |
import { Observable, throwError } from 'rxjs'; | |
import { catchError } from 'rxjs/operators'; | |
import { TokenService } from '../service/Authentication/token.service'; | |
import { ConfigurationDataService } from '../service/configuration.data.service'; | |
import { APP_CONFIG, IAppConfig } from '../config/app.config'; | |
@Injectable() | |
export class ServerConfigInitializer { | |
private result: Object = null; | |
constructor( @Inject(APP_CONFIG) private config: IAppConfig, | |
private configurationDataService: ConfigurationDataService, | |
private tokenService: TokenService) { | |
} | |
public getResult() { | |
return this.result; | |
} | |
public load() { | |
return new Promise((resolve, reject) => { | |
this.configurationDataService.getConfigurationSettings() | |
.pipe(catchError((error: any): any => { | |
reject(false); | |
return throwError(error.json().error || 'Server error'); | |
})).subscribe((data: any) => { | |
this.config.validatorMetaData = data.validatorMetaData; | |
this.config.environment = data.environment; | |
this.config.apiEndpoint = data.appApiUrl; | |
this.config.appAgencyWebUrl = data.appAgencyWebUrl; | |
this.config.appFinanceWebUrl = data.appFinanceWebUrl; | |
this.config.appCssFullUrl = data.appCssFullUrl; | |
this.config.appFeedbackMailTo = data.appFeedbackMailTo; | |
this.config.authConfig = data.authServiceConfig; | |
this.config.applicationVersionInfo = data.applicationVersionInfo; | |
resolve(true); | |
}); | |
}); | |
} | |
} | |
this provider will be execute when application is initializing, can set value to APP_CONFIG to share among the project |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment