Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.