Angular 7 TypeError:无法读取属性';创建组件&x27;未定义的

Angular 7 TypeError:无法读取属性';创建组件&x27;未定义的,angular,typeerror,angular7,angular-dynamic-components,Angular,Typeerror,Angular7,Angular Dynamic Components,我收到一封信 TypeError:无法读取未定义的属性“createComponent” 在尝试动态加载组件时 import { AfterViewInit, ViewChild, ViewContainerRef, Component, ComponentFactory, ComponentFactoryResolver, ComponentRef, OnDestroy, } from '@angular/core'; import { AddFabCompo

我收到一封信

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);
  }
}
我也试过不同的容器(以防万一)



我迷路了。任何代码示例都将不胜感激。

创建了Stackblitz演示,与您的场景相同。我使用角度7。它似乎没有触发与您遇到的错误相同的错误。我想这是因为我试图将组件添加到父级,而不是子级??
<div #fabconainter></div>
<ng-template #fabconainter></ng-template>
<template #fabconainter></template>