如何从 Angular 9 中的 ComponentFactoryResolver 访问 Angular7 中可用的 _factories 属性?

2024-04-11

I used 组件工厂解析器访问所有入口组件工厂,然后在 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,现在_工厂在 ComponentFactoryResolver 中不可用。

var 工厂 = this.componentFactoryResolver['_factories']

它在 Angular 9 中返回未定义。如果有人可以建议某种方法来随时随地访问所有条目组件列表,那将有很大帮助。问题是我没有任何关于入口组件的信息,我可以通过编译器重新编译组件并添加它们的路由。


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

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

constructor(private componentFactoryResolver: ComponentFactoryResolver) {
}

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

this.router.resetConfig(config);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 Angular 9 中的 ComponentFactoryResolver 访问 Angular7 中可用的 _factories 属性? 的相关文章

随机推荐