我正在尝试在应用程序模块导入其模块之前加载配置文件,以便在应用程序模块导入模块时可以使用配置文件。
目前,配置文件的加载发生在app模块的providers部分,该模块在imports部分之后加载。
这是我的代码:
import {BrowserModule} from '@angular/platform-browser';
import {APP_INITIALIZER, NgModule} from '@angular/core';
import {AppComponent} from './app.component';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {Routes, Router, RouterModule} from '@angular/router';
import {MatGridListModule, MatCardModule, MatMenuModule, MatIconModule, MatButtonModule} from '@angular/material';
import {LayoutModule} from '@angular/cdk/layout';
import {
IMqttMessage,
MqttModule,
IMqttServiceOptions
} from 'ngx-mqtt';
import {DatePipe} from '@angular/common';
import {AppConfigService} from './shared/services/app-config.service';
import {IAppConfig} from '../assets/config/app-config.module';
import {HttpClientModule} from '@angular/common/http';
import * as path from 'path';
import {hostname} from 'os';
export function initializeApp(appConfig: AppConfigService) {
console.log('init');
return () => appConfig.load();
}
export function MqttServiceOptionsConf(): IMqttServiceOptions {
console.log('init2');
const appConfiguration = AppConfigService.settings;
const mqttopt: IMqttServiceOptions = {hostname: appConfiguration.MqttProps['HostName'], port: appConfiguration.MqttProps['Port'], path: '/mqtt'};
return mqttopt;
}
const routes: Routes = [
{path: 'contactmanager', loadChildren: './contactmanager/contactmanager.module#ContactmanagerModule'},
{path: 'demo', loadChildren: './demo/demo.module#DemoModule'},
{path: '**', redirectTo: 'contactmanager'}
];
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
HttpClientModule,
MqttModule.forRoot(MqttServiceOptionsConf()),
BrowserAnimationsModule,
RouterModule.forRoot(routes),
MatGridListModule,
MatCardModule,
MatMenuModule,
MatIconModule,
MatButtonModule,
LayoutModule,
],
providers: [DatePipe,
AppConfigService,
{
provide: APP_INITIALIZER,
useFactory: initializeApp,
deps: [AppConfigService], multi: true
},
],
bootstrap: [AppComponent]
})
export class AppModule {
}
当试图加载MqttModule时,相关的部分在函数initializeApp,MqttServiceOptionsConf和imports中,以及在加载配置文件时在提供程序中。
有什么想法可以解决这个问题吗?