Created
March 24, 2019 16:00
-
-
Save Hypercubed/cf65d4442946f9016d84d3562f267824 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, Injector, Compiler } from '@angular/core'; | |
import { SettingsService } from './settings.service'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class PluginsService { | |
constructor( | |
private settings: SettingsService, | |
private injector: Injector, | |
private compiler: Compiler) { | |
} | |
initPlugins() { | |
this.settings.plugins.forEach(plugin => { | |
if (typeof plugin === 'function' && plugin.ngInjectorDef) { | |
this.loadModule(plugin); | |
} | |
if (typeof plugin === 'object' && plugin.ngModule) { | |
this.loadModule(plugin.ngModule, plugin.providers); | |
} | |
}); | |
} | |
private loadModule(module: any, providers?: any[]) { | |
this.compiler.compileModuleAsync<any>(module).then(moduleFactory => { | |
const injector = Injector.create({ | |
providers, | |
parent: this.injector | |
}); | |
moduleFactory.create(injector); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment