app.module.ts> APP_INITIALIZER依赖于另一个

时间:2019-05-19 23:28:17

标签: angular module

我需要一个APP_INITIALIZER才能从以前的APP_INITIALIZER服务获取数据。在这种情况下,要设置语言。

我有一个先前的初始化程序,可以重载

export function configLoaderFactory(provider: ConfigLoaderService) {
    return () => provider.load();
}

export function settingsLoaderFactory(provider: SettingsLoaderService) {
    return () => provider.load();
}

export function translateHttpLoaderFactory(http: HttpClient) {
    return new TranslateHttpLoader(http, './assets/i18n/');
}

@NgModule({
    declarations: [WpaComponent],
    imports: [
        BrowserModule,
        IonicModule.forRoot({
            mode: 'ios',
            rippleEffect: false,
            statusTap: false
        }),
        HttpClientModule,
        TranslateModule.forRoot({
            loader: {
                provide: TranslateLoader,
                useFactory: translateHttpLoaderFactory,
                deps: [HttpClient]
            }
        }),
        IonicStorageModule.forRoot(),
        FormsModule,
        ReactiveFormsModule,
        MbscModule,
        WpaDirectivesModule
    ],
    providers: [
        AppInjector,
        StatusBar,
        SplashScreen,
        AppVersion,

        Keyboard,

        {provide: RouteReuseStrategy, useClass: IonicRouteStrategy},


        ConfigLoaderService,
        {provide: APP_INITIALIZER, useFactory: configLoaderFactory, deps: [ConfigLoaderService, AppVersion], multi: true},

        SettingsLoaderService,
        {provide: APP_INITIALIZER, useFactory: settingsLoaderFactory, deps: [SettingsLoaderService, StorageService], multi: true},


        {
            provide: APP_INITIALIZER,
            useFactory: (translate: TranslateService, config: ConfigLoaderService) => {
                return () => {
 console.log(`config.getConfig()-> `, config.getConfig()); // This yields null because it is not loaded yet

    /*              if (config.getConfig().force_language) {

                        translate.setDefaultLang(config.getConfig().force_language_locale);
                        translate.use(config.getConfig().force_language_locale);

                    }


                };
            },
            multi: true,
            deps: [TranslateService, ConfigLoaderService]
        },

        AuthGuardService,
        ApiService
    ], exports: [
        BrowserModule,
        IonicModule,
        FormsModule,
        MbscModule,
        ReactiveFormsModule,
        HttpClientModule,
        IonicStorageModule,
        WpaDirectivesModule,
        TranslateModule
    ]
})

我无法访问ConfigLoaderService。

是否可以在ConfigLoaderService中分阶段加载或等待文件(这是一个小的json文件)加载?

0 个答案:

没有答案