Angular:如何通过 Injector 在 ngComponentOutlet 中传递数据

Unk*_*ist 4 angular

我正在尝试动态创建和显示组件,我需要将一些数据传递给它,以便它知道要显示什么。

这是我的代码:

html部分:

<div class="basicContainer">
  <div class="projectsTreeContainer">


    <input type="text" id="searchWord" placeholder="Search through projects"/>

    <button (click)="loadAddProject()">Add new Project</button>

    <app-projects-tree (onLoadProjectDetails)="loadProjectDetails($event)"
                       (onLoadWpDetails)="loadWpDetails($event)"
                       (onSelectAndLoadJobDetails)="loadJobDetails($event)"></app-projects-tree>
  </div>

  <div class="infoContainer">
    <ng-container *ngComponentOutlet="details"></ng-container>
  </div>

</div>
Run Code Online (Sandbox Code Playgroud)

成分:

export class ProjectsComponent implements OnInit {

  details: Component;
  private showWp: boolean;

  constructor() {
  }


  loadProjectDetails(project: BasicProject): void {
     this.details = new ProjectComponent(project);
  }
Run Code Online (Sandbox Code Playgroud)

以及我想要动态创建和显示的组件:

export class ProjectComponent implements OnInit {

  project: Project;

  constructor(basicProject: BasicProject) {
    this.project = new Project();
    this.project.name = basicProject.name ;
  }
Run Code Online (Sandbox Code Playgroud)

Ng 模块:

@NgModule({
  declarations: [
    AppComponent,
    NavbarComponent,
    LoginComponent,
    ProjectsComponent,
    ProjectComponent,
    ProjectsTreeComponent,
    TreeProjectComponent,
    TreeWpComponent,
    WpComponent,
    JobComponent,
    AddProjectComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    RouterModule.forRoot(
      AppRoutes.appRoutes,
      // {enableTracing: true} // <-- debugging purposes only
    ),
    HttpClientModule,
    HttpModule
  ],
  providers: [
    AuthenticationService,
    ProjectsService,
    CanActivateAuthGuard,
    {
      provide: HTTP_INTERCEPTORS,
      useClass: AuthInterceptor,
      multi: true
    }],
  bootstrap: [AppComponent],
  entryComponents: [ProjectComponent, WpComponent, JobComponent, AddProjectComponent]
})
Run Code Online (Sandbox Code Playgroud)

错误信息:

ProjectsComponent.html:15 ERROR 错误:找不到 [object Object] 的组件工厂。

我怎样才能完成任务?谢谢。

yur*_*zui 11

如果打开NgComponentOutlet指令代码:

@Directive({selector: '[ngComponentOutlet]'})
export class NgComponentOutlet implements OnChanges, OnDestroy {
  @Input() ngComponentOutlet: Type<any>;
  @Input() ngComponentOutletInjector: Injector;
Run Code Online (Sandbox Code Playgroud)

您可以注意到它需要ngComponentOutlet输入应该具有类型Type<any>但您将对象传递给它。

我们也可以看到这个指令可以Injector作为@Input. 因此,让我们利用这些知识来完成您的任务。

例如,我们编写的模板如下:

<ng-container *ngComponentOutlet="component; injector: injector"></ng-container>
Run Code Online (Sandbox Code Playgroud)

现在让我们在组件类中声明componentinjector属性:

@Component({...})
export class AppComponent  {
  
  component: Type<any>;
  injector: Injector;

  constructor(private inj: Injector) {}

  ngOnInit() {
    this.component = ProjectComponent; // note: we're passing type here
    this.injector = Injector.create([
      { provide: BasicProject, useValue: new Project('test name') }
    ], this.inj);
  }
}
Run Code Online (Sandbox Code Playgroud)

并且您ProjectComponent现在使用 angular DI 机制来获取我们传递给注入器的数据:

export class ProjectComponent {
  name: string;

  constructor(project: BasicProject) {
    this.name = project.name;
  }
}
Run Code Online (Sandbox Code Playgroud)

Stackblitz 示例