mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-23 22:17:40 +08:00
fix(angular): Config provider
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
import { ModuleWithProviders, NgModule } from '@angular/core';
|
import { InjectionToken, ModuleWithProviders, NgModule } from '@angular/core';
|
||||||
import { CommonModule } from '@angular/common';
|
import { CommonModule } from '@angular/common';
|
||||||
|
|
||||||
import * as c from './directives';
|
import * as c from './directives';
|
||||||
@ -119,7 +119,7 @@ const PROVIDERS = [
|
|||||||
p.NavController,
|
p.NavController,
|
||||||
p.Platform,
|
p.Platform,
|
||||||
p.Events,
|
p.Events,
|
||||||
p.DomController
|
p.DomController,
|
||||||
];
|
];
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
@ -135,12 +135,22 @@ const PROVIDERS = [
|
|||||||
]
|
]
|
||||||
})
|
})
|
||||||
export class IonicModule {
|
export class IonicModule {
|
||||||
static forRoot(): ModuleWithProviders {
|
static forRoot(config?: {[key: string]: any}): ModuleWithProviders {
|
||||||
return {
|
return {
|
||||||
ngModule: IonicModule,
|
ngModule: IonicModule,
|
||||||
providers: [
|
providers: [
|
||||||
|
// Load config with defualt values
|
||||||
|
{ provide: ConfigToken, useValue: config },
|
||||||
|
{ provide: p.Config, useFactory: setupConfig, deps: [ ConfigToken ] },
|
||||||
|
|
||||||
...PROVIDERS,
|
...PROVIDERS,
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const ConfigToken = new InjectionToken<any>('USERCONFIG');
|
||||||
|
export function setupConfig(userConfig: any): p.Config {
|
||||||
|
const config = new p.Config(userConfig);
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
40
angular/src/providers/config.ts
Normal file
40
angular/src/providers/config.ts
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import { Config as CoreConfig } from '@ionic/core';
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
getBoolean(key: string, fallback?: boolean): boolean {
|
||||||
|
return getConfig().getBoolean(key, fallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
getNumber(key: string, fallback?: number): number {
|
||||||
|
return getConfig().getNumber(key, fallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
set(key: string, value?: any) {
|
||||||
|
getConfig().set(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getConfig(): CoreConfig {
|
||||||
|
const Ionic = (window as any).Ionic;
|
||||||
|
if (Ionic && Ionic.config) {
|
||||||
|
return Ionic.config;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
@ -12,3 +12,4 @@ export { PopoverController } from './popover-controller';
|
|||||||
export { ToastController } from './toast-controller';
|
export { ToastController } from './toast-controller';
|
||||||
export { NavController } from './nav-controller';
|
export { NavController } from './nav-controller';
|
||||||
export { DomController } from './dom-controller';
|
export { DomController } from './dom-controller';
|
||||||
|
export { Config } from './config';
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
|
|
||||||
import { PlatformConfig } from '@ionic/core';
|
import { PlatformConfig } from '@ionic/core';
|
||||||
import { HostListener } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
export class Platform {
|
export class Platform {
|
||||||
|
|
||||||
private _platforms: PlatformConfig[] = [];
|
private _platforms: PlatformConfig[] = [];
|
||||||
|
Reference in New Issue
Block a user