Angular 7 TypeError:无法读取未定义的属性“ createComponent”

时间:2018-12-07 00:27:51

标签: angular typeerror angular7 angular-dynamic-components

我收到

  

TypeError:无法读取未定义的属性'createComponent'

尝试动态加载组件时。

import {
  AfterViewInit,
  ViewChild,
  ViewContainerRef,
  Component,
  ComponentFactory,
  ComponentFactoryResolver,
  ComponentRef,
  OnDestroy,
} from '@angular/core';

import { AddFabComponent } from '@app/shared/components/fabs/add-fab/add-fab.component'


@Component({
  selector: 'app-airframe-list',
  templateUrl: './airframe-list.component.html',
  styleUrls: ['./airframe-list.component.scss']
})

export class AirframeListComponent implements AfterViewInit, OnDestroy {

  componentRef: any;
  destroy: any;

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

  constructor(private resolver: ComponentFactoryResolver) { }

  ngAfterViewInit() {
    this.createComponent();
  }

  ngOnDestroy() {
    this.componentRef.destroy();
  }

  createComponent() {
      const factory = this.resolver.resolveComponentFactory(AddFabComponent);
      const componentRef = this.fabconainter.createComponent(factory);
  }
}

我也尝试过使用其他容器(以防万一)

<div #fabconainter></div>
<ng-template #fabconainter></ng-template>
<template #fabconainter></template>

我很茫然。任何代码示例也将不胜感激。

0 个答案:

没有答案