0

我想动态处理模块

不仅在demo上绘制demoule中的组件,还绘制imports中的组件

除了 impont 模块,我想在 ViewChild 中执行此操作。我想在 ngModuleFactory 中打开 moduleType。我有一个错误。

这个.module

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common'
import { FlightBookingDetailComponent } from './flight-booking-detail.component';
import { TabModule } from 'app/shared/custom-directives/tab/tab.module';

@NgModule({
  declarations: [FlightBookingDetailComponent],
  imports: [
    CommonModule,
    TabModule
  ]
})
export class FlightBookingDetailModule { }

模态组件.ts

import {Compiler, Component, Type, Input, Injector, NgModule, NgModuleRef, ComponentFactoryResolver, ViewChild, ViewContainerRef, NgModuleFactory} from "@angular/core";
import { ModalDirective } from './modal.directive';

@Component({
    selector: 'agent-modal',
    template: '<div #vc></div>'
})
export class ModalComponent {
  @Input() component: Type<any>;
  @Input() module: Type<any>;

  @ViewChild('vc', {read: ViewContainerRef}) container: ViewContainerRef;

  constructor(private compiler: Compiler,
              private componentFactoryResolver: ComponentFactoryResolver,
              private injector: Injector,
              private ngModuleRef: NgModuleRef<any>) {
  }

  ngAfterViewInit() {
    const template = '<span>I am {{name}}</span>';

    console.log('aaa', this.module)
    this.compiler.compileModuleAndAllComponentsAsync(this.module)
    .then((factories) => {
      console.log('asfadf', factories)
      const moduleType = factories.ngModuleFactory.moduleType() << error



      for (let componentFactories of factories.componentFactories) {
        const cmpRef = componentFactories.create(this.injector, [], null, this.ngModuleRef);

        console.log('cmpRef', cmpRef)

        this.container.insert(cmpRef.hostView);
      }
    })
  }
}

错误:'类型'Type'的值不可调用。你的意思是包括'新'?ts(2348)'

4

0 回答 0