This question relates directly to a public Angular API: https://angular.io/api/core/ViewContainerRef#insert
I am dynamically loading components from their ngFactories via this code.
I have upgraded from Angular 8.2 to 9.1 and seeing some errors in the way my components are loaded into a view. This code was working perfectly fine until the upgrade to angular 9.1:
// Create a view for the component to be inserted into
@ViewChild('myView', { read: ViewContainerRef, static: true }) myView: ViewContainerRef;
// Grab dynamic module
let moduleFactory: NgModuleFactory<any> = myNgFactory;
// Create reference using the current code's injector
let moduleReference = moduleFactory.create(this.injector);
// Grab the component factory from the module
let componentFactory = moduleReference.componentFactoryResolver.resolveComponentFactory(myComponentFactory);
// Create the component
let component = componentFactory.create(moduleReference.injector);
// Then insert into myView
this.myView.insert(component.hostView);
The error happens on the last line, when trying to run the ViewContainerRef.insert
Error:
ERROR TypeError: Cannot read property '1' of undefined
at ViewContainerRef.insert(core.js:15711)
...
The error happens here in the console (compiled core):
insert(viewRef, index) {
/** @type {?} */
const lView = (/** @type {?} */(((/** @type {?} */(viewRef)))._lView));
const tView = lView[TVIEW];
^^^^^^^^
...
}
I found higher in the core.js file that the const TVIEW = 1
The direct Angular code reference (code has not changed from Angular 8 to 9 as far as I can tell): https://github.com/angular/angular/blob/9.1.x/packages/core/src/view/refs.ts#L207
My Component object that I am trying to insert (I do see a small difference in the component, is this the cause?):
I found a similar question but I am receiving a different error: Angular 9 isssue with dynamic component load
I was hoping posting would allow folks to share some ideas or point me in the right direction where things are going wrong or if this is even possible in the Angular 9 Ivy compiler? Any help on this is much apperciated!