Вы можете создать generic.module.ts, которые будут иметь оба компонента в объявлениях массива:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { UserDashboardComponent } from './user-dashboard.component'
import { AdminDashboardComponent } from './admin-dashboard.component
@NgModule({
imports: [ BrowserModule ],
declarations: [ UserDashboardComponent,AdminDashboardComponent ]
})
export class GenericModule { }
Таким образом, у вас будет модуль, содержащий модули, которые вы хотите загрузить.
Теперь следующий шаг будет загружать их асинхронны с помощью компилятора: внутри компоненты сделайте следующее:
import {GenericModule} from './generic.module';
import { Component, Input,ViewContainerRef, Compiler, NgModule,ModuleWithComponentFactories,OnInit,ViewChild} from '@angular/core';
@Component({
selector: 'generic',
template: '<div #target></div>'
})
export class App implements AfterViewInit {
@ViewChild('target', {read: ViewContainerRef}) target: ViewContainerRef;
constructor(private compiler: Compiler) {}
ngAfterViewInit() {
this.createComponent('<u>Example template...</u>');
}
private createComponent(template: string,role:string) {
@Component({template: template});
const mod = this.compiler.compileModuleAndAllComponentsSync(GenericModule);
const factory = mod.componentFactories.find((comp) =>
//you can add your comparison condition here to load the component
//for eg. comp.selector===role where role='admin'
);
const component = this.target.createComponent(factory);
}
}
Надеется, что это помогает.
[Нажмите здесь] (https://angular.io/docs/ts/latest/guide/router.html) и найдите «Конфигурация маршрута ленивого загрузки» – cvsguimaraes
Lazy loading не помогает здесь. См. Мой комментарий к @ user32 answer. –
Вы поняли это? – Dmitry