Angular Universal:在应用程序初始化器之前初始化的延迟加载模块
在初始化应用程序之前,我的应用程序使用Angular Universal:在应用程序初始化器之前初始化的延迟加载模块,angular,angular-universal,Angular,Angular Universal,在初始化应用程序之前,我的应用程序使用app\u INITIALIZER从json文件中动态检索一些配置 当使用angular universal时,在使用APP_初始值设定项时返回的承诺值解决之前,会在服务器端创建延迟加载的模块 当不使用角度通用时,这可以正常工作。这是angular universal的错误,还是我做错了什么 应用程序模块.ts export function loadConfigService(configService: AppConfigService): Functi
app\u INITIALIZER
从json文件中动态检索一些配置
当使用angular universal时,在使用APP_初始值设定项时返回的承诺值解决之前,会在服务器端创建延迟加载的模块
当不使用角度通用时,这可以正常工作。这是angular universal的错误,还是我做错了什么
应用程序模块.ts
export function loadConfigService(configService: AppConfigService): Function
{
return () => {
return configService.load();
};
}
@NgModule({
//...
providers: [
{
provide: APP_INITIALIZER,
useFactory: loadConfigService,
deps: [AppConfigService],
multi: true
},
export class AppConfig
{
readonly apiUrl: string;
//other properties
}
export let APP_CONFIG: AppConfig;
@Injectable()
export class AppConfigService
{
constructor(private http: HttpClient, @Inject(PLATFORM_ID) private platformId: Object, @Optional() @Inject('serverUrl') protected serverUrl: string)
{
}
public load()
{
console.log('in load');
return new Promise((resolve, reject) => {
const isBrowser = isPlatformBrowser(this.platformId);
let baseUrl = isBrowser ? '' : this.serverUrl;
this.http.get(`${baseUrl}/assets/config/conf.json`).pipe(catchError((error: any): any => {
reject(error);
return observableThrowError('Server error');
})).subscribe((envResponse: any) => {
let t = new AppConfig();
APP_CONFIG = Object.assign(t, envResponse);
console.log('in response');
resolve(true);
});
});
}
}
app config.service.ts
export function loadConfigService(configService: AppConfigService): Function
{
return () => {
return configService.load();
};
}
@NgModule({
//...
providers: [
{
provide: APP_INITIALIZER,
useFactory: loadConfigService,
deps: [AppConfigService],
multi: true
},
export class AppConfig
{
readonly apiUrl: string;
//other properties
}
export let APP_CONFIG: AppConfig;
@Injectable()
export class AppConfigService
{
constructor(private http: HttpClient, @Inject(PLATFORM_ID) private platformId: Object, @Optional() @Inject('serverUrl') protected serverUrl: string)
{
}
public load()
{
console.log('in load');
return new Promise((resolve, reject) => {
const isBrowser = isPlatformBrowser(this.platformId);
let baseUrl = isBrowser ? '' : this.serverUrl;
this.http.get(`${baseUrl}/assets/config/conf.json`).pipe(catchError((error: any): any => {
reject(error);
return observableThrowError('Server error');
})).subscribe((envResponse: any) => {
let t = new AppConfig();
APP_CONFIG = Object.assign(t, envResponse);
console.log('in response');
resolve(true);
});
});
}
}
子模块
import {APP_CONFIG} from "@utils/services/app-config.service";
export class LazyChildModule
{
constructor()
{
console.log('in child constructor');
//Here, APP_CONFIG is null
不带通用接口的输出:
in load
in response
in child constructor
in load
in child constructor
in response
具有通用性的输出:
in load
in response
in child constructor
in load
in child constructor
in response
这仍然是一个问题,你解决了吗?@ayyash我必须使用一个变通方法,配置服务在准备就绪时发出一个事件,这样我才能在子模块中使用config对象。