これで、AoTを使用せずに動的にコンポーネントをロードするモジュールを作成しようとしていました。これは挑戦であることが証明されていますが、私は本当に近くになっています。 ComponentFactoryに格納されているすべてのコンポーネントを取得するにはどうすればよいですか?私の動的ロードモジュールのAoTを壊すことなくコンポーネントを動的にロードする - Angular2
MIJ app.module.ts
IにおいてforRoot(の構成要素の配列を渡す):私のdynamic-load.module.ts
で
imports: [
DynamicLoadModule.forRoot(dynamicComponents),
]
IはAngular2 ComponentFactoryにそれらのすべてを追加します。
static forRoot(components: any[]) {
return {
ngModule: DynamicLoadModule,
providers: [{
provide: ANALYZE_FOR_ENTRY_COMPONENTS,
useValue: components,
multi: true,
}, {
provide: DynamicLoadService,
useValue: new DynamicLoadService(components)
}]
};
}
問題はDynamicLoadService
にあります。これはAoTを乱用しています。私は文書化されたエクスポート関数を試してみましたが、この方法ではサービスにパラメータとしてcomponents
を渡すことができないため、これで問題は解決されません。その後、私はその後、コンパイルされていないコンポーネントを持っていたので役に立たないのもありましたOpaqueToken
の使用によって私のdynamic-load.module
に成分を通過しようとしたので、
let heroServiceFactory = (logger: Logger, userService: UserService) => {
return new HeroService(logger, userService.user.isAuthorized);
};
export let heroServiceProvider =
{ provide: HeroService,
useFactory: heroServiceFactory,
deps: [Logger, UserService]
};
:だからthe Angular documentationで見つかった以下は、私の問題のためのソリューションを提供していません私のモジュールは多くの柔軟性を失いました。
私は結論に達しました。ComponentFactory
のすべてのコンポーネントの配列が必要な場合は、それらをすべて取得する方法が必要です。
ここで、ComponentFactoryResolver
を使用して名前に基づいて1つのテンプレートを取得すると、それらをすべて取得するか、少なくとも工場のコンポーネントのすべての名前を取得する必要があります。
if (selectedComponent !== '') {
if (typeof this.currentComponent !== 'undefined') { this.currentComponent.destroy(); }
const components = this.dynamicLoadService.getComponents(), // --> instead of getting a list of available components through this service which needs the factory that blocks AoT, I should just be able to retrieve all available components from the ComponentFactory
component: any = components.find((x) => x.name.toLowerCase().indexOf(selectedComponent) > -1),
compFactory = this.cfr.resolveComponentFactory(component);
this.currentComponent = this.vcr.createComponent(compFactory);
this.currentComponent.instance.data = this.componentData;
}