7

in my angular app I have the following:

export class MyComponent {
    subcompPath = "path-to-subcomp#SubcompClassName";
    @ViewChild("placeholder", { read: ViewComponentRef }) placeholderRef: ViewComponentRef;

/* Constructor where Compiler, ComponentFactoryResolver are injected */

    loadSubcomponent() {
        let [path, componentName] = this.subcompPath.split("#");
        (<any>window).System.import(path)
            .then((module: any) => module[componentName])
            .then((type: any) => {
                return this._compiler.compileComponentAsync(type)
            })
            .then((factory: any) => {
                let componentRef = this.placeholderRef.createComponent(factory, 0);
            });
    }
}

My sub-component declares providers and stuff, directives and pipes.

And now RC6 is out to break everything yet again. Components can't declare directives and pipes, but they must be in the module where the component is declared. So I have to load with SystemJS not the component itself but the module. Ok, and then I should use

return this._compiler.compileModuleAndAllComponentsAsync(type)

Fine, but how do I get a reference to the factory of this specific component? That factory is all I need, the placeholderRef wants it in its createComponent method, right?

I tried to dig into the angular2 source code from github but it's quite vast, I should try from VS Code or something, with intellisense, but I'm lazy...and I should read this stuff from documentation, which is quite lackluster in angular.io for this particular argument, which is lazy loading of components and modules WITHOUT the router.

Any help is appreciated, I think the solution is simple to apply but hard to find without official documentation.

yurzui
  • 205,937
  • 32
  • 433
  • 399
Etchelon
  • 832
  • 11
  • 27

2 Answers2

9

Update:

If you want to use it together with aot compilation you should manually provide Compiler like

export function createJitCompiler () {
   return new JitCompilerFactory([{useDebug: false, useJit: true}]).createCompiler();
}
...
providers: [
  { provide: Compiler, useFactory: createJitCompiler}
],

Example

Old version

It might help you:

this.compiler.compileModuleAndAllComponentsAsync(DynamicModule)
      .then(({moduleFactory, componentFactories}) => {
        const compFactory = componentFactories
           .find(x => x.componentType === DynamicComponent);
        const cmpRef = this.placeholderRef.createComponent(compFactory, 0);

See also

yurzui
  • 205,937
  • 32
  • 433
  • 399
  • Thx @yurzui for the pointer, but the solution to my problem is actually: `const compFactory = moduleWithComponentFactory.componentFactories .find(x => x.componentType.name === componentName);` Because I want to find the component dynamically by string, since I don't know during development the type, and I'm not creating a type dynamically...I know the type name because the type has been developed already somewhere in a plugin to the application – Etchelon Sep 05 '16 at 09:33
8

Based on yurzui's answer I've come to the following code:

export class MyComponent {
    subcompPath = "path-to-subcompMODULE#SubcompClassName";
    @ViewChild("placeholder", { read: ViewComponentRef }) placeholderRef: ViewComponentRef;

    /* Constructor where Compiler, ComponentFactoryResolver are injected */

    loadSubcomponent() {
        let [modulePath, componentName] = this.subcompPath.split("#");
        (<any>window).System.import(modulePath)
            .then((module: any) => module["default"])  // Or pass the module class name too
            .then((type: any) => {
                return this._compiler.compileModuleAndAllComponentsAsync(type)
            })
            .then((moduleWithFactories: ModuleWithComponentFactories<any>) => {
                const factory = moduleWithFactories.componentFactories.find(x => x.componentType.name === componentName); // Crucial: componentType.name, not componentType!!
                let componentRef = this.placeholderRef.createComponent(factory, 0);
            });
    }
}
Etchelon
  • 832
  • 11
  • 27