简体   繁体   中英

Angular 4 ngComponentOutlet and lazy loading modules

I am currently starting to get the hang of angular 4 , specifically the dynamic components part.

I managed to replicate with success the ngComponentOutlet approach described here , plunkr here

Now what i would like to do is lazy loading a module containing the dynamic component. I see on Github that this feature should have been implemented but i'm not experienced enough in angular 4 to interpret the source changes myself, and i can't find anywhere an example of this approach.

Also, i'd like to achieve this kind of lazy loading without depending on the a4 router and with webpack (if it matters).

Anybody willing to help?

TIA

You can use the method compileModuleAndAllComponentsAsync of the compiler. Something along these lines:

ngAfterViewInit() {
  System.import('app/t.module').then((module) => {
      _compiler.compileModuleAndAllComponentsAsync(module.TModule)
        .then((compiled) => {
          const m = compiled.ngModuleFactory.create(this._injector);
          const factory = compiled.componentFactories[0];
          const cmp = factory.create(this._injector, [], null, m);
        })
    })
}

For more information read Here is what you need to know about dynamic components in Angular .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM