How to access _factories property from ComponentFactoryResolver in Angular9 which was available in Angular7?

独自空忆成欢 提交于 2021-01-28 21:46:09

问题


I used ComponentFactoryResolver to access all the entry component factories and then add routes of those components dynamically in Angular 7.

constructor(private componentFactoryResolver: ComponentFactoryResolver) {
}

var factories = this.componentFactoryResolver['_factories']

let route: Route = {
                path: MyPath,
                component: factories[0].factory[1].componentType
            };

this.router.resetConfig(config);

I updated my project to Angular 9 and now _factories are not available in ComponentFactoryResolver.

var factories = this.componentFactoryResolver['_factories']

It returns undefined in Angular 9. It will be great help if someone can suggest some way to access all the entry component list on the go. The problem is that I have don't any information available about the entry components from which I may recompile the components through Compiler and add their routes.


回答1:


I think a simpler approach might be to import the component and use it directly in place of factories[0].factory[1].componentType. Example:

import { MyComponent } from '@app/my.component';

constructor(private componentFactoryResolver: ComponentFactoryResolver) {
}

let route: Route = {
                path: MyPath,
                component: MyComponent
            };

this.router.resetConfig(config);


来源:https://stackoverflow.com/questions/61707741/how-to-access-factories-property-from-componentfactoryresolver-in-angular9-whic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!