fix(angular): Config provider

This commit is contained in:
Manu Mtz.-Almeida
2018-04-27 01:44:43 +02:00
parent 98a351941d
commit 329a34892b
2 changed files with 6 additions and 23 deletions

View File

@@ -4,6 +4,7 @@ import { CommonModule } from '@angular/common';
import * as c from './directives';
import * as d from './directives/proxies';
import * as p from './providers';
import { setupConfig } from '@ionic/core';
const DECLARATIONS = [
@@ -120,6 +121,7 @@ const PROVIDERS = [
p.Platform,
p.Events,
p.DomController,
p.Config
];
@NgModule({
@@ -136,21 +138,12 @@ const PROVIDERS = [
})
export class IonicModule {
static forRoot(config?: {[key: string]: any}): ModuleWithProviders {
setupConfig(config);
return {
ngModule: IonicModule,
providers: [
// Load config with defualt values
{ provide: ConfigToken, useValue: config },
{ provide: p.Config, useFactory: setupConfig, deps: [ ConfigToken ] },
...PROVIDERS,
]
providers: PROVIDERS
};
}
}
export const ConfigToken = new InjectionToken<any>('USERCONFIG');
export function setupConfig(userConfig: any): p.Config {
const config = new p.Config(userConfig);
return config;
}

View File

@@ -1,19 +1,9 @@
import { Config as CoreConfig } from '@ionic/core';
import { Injectable } from '@angular/core';
@Injectable()
export class Config {
constructor(defaultConfig: {[key: string]: any}) {
const Ionic = (window as any).Ionic;
if (Ionic.config && Ionic.config.constructor.name !== 'Object') {
console.error('ionic config was already initialized');
return;
}
Ionic.config = {
...Ionic.config,
...defaultConfig
};
}
get(key: string, fallback?: any): any {
return getConfig().get(key, fallback);
}