如何将ViewContainerRef与Angular 9中工厂的动态组件一起使用?
此问题直接与公共角度API有关: 我通过这段代码从他们的NGF工厂动态加载组件 我已经从Angular 8.2升级到了9.1,在将组件加载到视图的方式中发现了一些错误。在升级到angular 9.1之前,该代码一直运行良好:如何将ViewContainerRef与Angular 9中工厂的动态组件一起使用?,angular,angular9,viewchild,ngfactory,Angular,Angular9,Viewchild,Ngfactory,此问题直接与公共角度API有关: 我通过这段代码从他们的NGF工厂动态加载组件 我已经从Angular 8.2升级到了9.1,在将组件加载到视图的方式中发现了一些错误。在升级到angular 9.1之前,该代码一直运行良好: // Create a view for the component to be inserted into @ViewChild('myView', { read: ViewContainerRef, static: true }) myView: ViewContain
// 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);
错误发生在控制台(编译核心)中:
我在core.js文件中发现const TVIEW=1
直接角度代码参考(据我所知,代码没有从角度8更改为角度9):
我正在尝试插入的组件对象(我确实看到组件中有一个小差异,这是原因吗?)
我发现了一个类似的问题,但我收到了一个不同的错误:
我希望这篇帖子能让大家分享一些想法,或者告诉我哪里出了问题,或者这在Angular 9 Ivy编译器中是否可行?这方面的任何帮助都是非常诱人的 我做了一些调查,找出了我的问题。
lView
在我的引用中不存在,因为我在Tconfig中编译组件/模块时关闭了常春藤,但没有在我加载它的环境中:
"angularCompilerOptions": {
"enableIvy": false
}
现在,在我动态加载这些组件的环境中,关闭Ivy后,它也能正常工作。我做了一些挖掘,并解决了我的问题。
lView
在我的引用中不存在,因为我在Tconfig中编译组件/模块时关闭了常春藤,但没有在我加载它的环境中:
"angularCompilerOptions": {
"enableIvy": false
}
现在,在我动态加载这些组件的环境中关闭Ivy后,它也能正常工作。Angular 9默认使用AOT和Ivy。你有没有试图禁用Ivy并使用JIT编译来排除它们?@ihorbond你的评论帮助我认识到我的问题。我在模块创建中禁用了Ivy,但在我想要动态拉入它的环境中没有。在tsconfig:
angularCompilerOptions:{enablevy:false}
Angular 9默认使用AOT和Ivy。你有没有试图禁用Ivy并使用JIT编译来排除它们?@ihorbond你的评论帮助我认识到我的问题。我在模块创建中禁用了Ivy,但在我想要动态拉入它的环境中没有。在tsconfig:angularCompilerOptions:{enableIvy:false}
"angularCompilerOptions": {
"enableIvy": false
}