Angular无法读取属性';创建';未定义的

Angular无法读取属性';创建';未定义的,angular,typescript,Angular,Typescript,我正在使用Angular 6,我想根据实现组件的动态添加,但我得到一个错误:“无法读取未定义的属性‘create’。 component.html <mat-card> <mat-card-header> <mat-card-title>...</mat-card-title> </mat-card-header> <mat-card-content> <ng-template answer

我正在使用Angular 6,我想根据实现组件的动态添加,但我得到一个错误:“无法读取未定义的属性‘create’。

component.html

<mat-card>
  <mat-card-header>
    <mat-card-title>...</mat-card-title>
  </mat-card-header>
  <mat-card-content>
    <ng-template answerHost></ng-template>
  </mat-card-content>
</mat-card>
<mat-card>
  <mat-card-header>
    <mat-card-title>...</mat-card-title>
  </mat-card-header>
  <mat-card-content>
    <ng-template #answerContainer></ng-template>
  </mat-card-content>
</mat-card>
我试图用对组件id的引用替换该指令,但得到了相同的错误。 component.html

<mat-card>
  <mat-card-header>
    <mat-card-title>...</mat-card-title>
  </mat-card-header>
  <mat-card-content>
    <ng-template answerHost></ng-template>
  </mat-card-content>
</mat-card>
<mat-card>
  <mat-card-header>
    <mat-card-title>...</mat-card-title>
  </mat-card-header>
  <mat-card-content>
    <ng-template #answerContainer></ng-template>
  </mat-card-content>
</mat-card>

确保您正在“ngAfterViewInit()”内调用此方法“changeStep”。

尝试使用ng Container更改ng模板同样的错误。我尝试从ngOninit和ngAfterViewInit调用方法changeStep。结果是相同的。创建了堆栈溢出,其工作正常。注意,我使用的是createComponent和Angular 6。您的示例是CreativeEmbededViev和Angular 2。我能够访问ViewContainerRef实例类可用的所有公共方法。i、 e clear(),createEmbeddedView()。createComponent()也可以工作。我已经用createComponent方法更新了stackblitz
@ViewChild('answerContainer', { read: ViewContainerRef }) answerContainer: ViewContainerRef;
private stepAnswerType1: ComponentFactory<StepAnswerType1Component>;
private stepAnswerType2: ComponentFactory<StepAnswerType2Component>;
private stepAnswerType3: ComponentFactory<StepAnswerType3Component>;

/* ... */

changeStep(id: number) {
    if (this.step.type == 1) {
      componentRef = this.answerContainer.createComponent(this.stepAnswerType1);
    }
    if (this.step.type == 2) {
      componentRef = this.answerContainer.createComponent(this.stepAnswerType2);
    }
    if (this.step.type == 3) {
      componentRef = this.answerContainer.createComponent(this.stepAnswerType3);
    }
}
import { StepAnswerType1Component } from './step-answer-type1/step-answer-type1.component';
import { StepAnswerType2Component } from './step-answer-type2/step-answer-type2.component';
import { StepAnswerType3Component } from './step-answer-type3/step-answer-type3.component';

/* some code */

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

/* some code */

changeStep(id: number) {
    let componentRef, componentFactory;
    if (this.step.type == 1) {
      componentFactory = this.componentFactoryResolver.resolveComponentFactory(StepAnswerType1Component);
    }
    if (this.step.type == 2) {
      componentFactory = this.componentFactoryResolver.resolveComponentFactory(StepAnswerType2Component);
    }
    if (this.step.type == 3) {
      componentFactory = this.componentFactoryResolver.resolveComponentFactory(StepAnswerType3Component);
    }
    componentRef = viewContainerRef.createComponent(componentFactory);
 }