In order to create block component dynamically, it is important to import following AppComponent such as-

  • ViewContainerRef, ComponentFactoryResolver and ViewChild from @angular/core
  • MessageComponent from message.component.
  • ComponentFactory and ComponentRef from@angular/core
BY Best Interview Question ON 08 Sep 2019

Example

import {
   Component,
   ViewContainerRef,
   ViewChild,
  ComponentFactoryResolver,
  ComponentRef,
  ComponentFactory
}

from '@angular/core';
import { MessageComponent } from './message.component';
@Component(
{
   selector: 'app-root',
   templateUrl: './app.component.html'
}
)
export class AppComponent {
  title = 'app';
}