如何从Angular7中可用的Angular9中的ComponentFactoryResolver访问_factories属性? [英] How to access _factories property from ComponentFactoryResolver in Angular9 which was available in Angular7?

查看:18
本文介绍了如何从Angular7中可用的Angular9中的ComponentFactoryResolver访问_factories属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ComponentFactoryResolver 访问所有入口组件工厂,然后在 Angular 7 中动态添加这些组件的路由.

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);

我将我的项目更新到 Angular 9,现在 _factories 在 ComponentFactoryResolver 中不可用.

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

var factory = this.componentFactoryResolver['_factories']

var factories = this.componentFactoryResolver['_factories']

它在 Angular 9 中返回 undefined.如果有人可以提出某种方法来随时随地访问所有入口组件列表,这将是非常有帮助的.问题是我没有关于入口组件的任何可用信息,我可以从中通过编译器重新编译组件并添加它们的路由.

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.

推荐答案

我认为更简单的方法可能是导入组件并直接使用它代替 factories[0].factory[1].componentType.示例:

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);

这篇关于如何从Angular7中可用的Angular9中的ComponentFactoryResolver访问_factories属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆