4

Here is what i'm trying de achieve : I want to create an app using "plugins", by plugins I mean :

Another angular component/module that could be deployed on a remote host and displayed in my app.

Looks like I can't do such thing with webpack directly and that I should use SystemJs to dynamically load a module.

Any advice would be welcome to use SystemJs and Webpack (ng cli), examples of how to call remote module and load them.

Seb
  • 3,602
  • 8
  • 36
  • 52
  • have you been able to solve the problem? – maximz101 Oct 06 '17 at 09:41
  • Yes but not using systemjs at all – Seb Oct 06 '17 at 10:03
  • Am trying to accomplish the same thing, would you be kind and post an answer or provide the steps that worked for you? thanks. – maximz101 Oct 06 '17 at 10:07
  • I need to make a github project, once I have few minutes but the idea is : create your plugin as an angular app, bundle it as an UMD, in your main app do a simple get to retrieve the plugin bundle and use "classic" dynamic component use as describe on angular website – Seb Oct 06 '17 at 11:57
  • what do you mean by a simple get? a simple HTTP get request? how do you parse the module then with its dependencies? I really appreciate your help as am not able to make it work. – maximz101 Oct 06 '17 at 13:24
  • 1
    have a look at this repo, this will help : https://github.com/Toxicable/module-loading – Seb Oct 10 '17 at 09:45
  • 1
    maybe this will help: https://stackoverflow.com/questions/50149016/load-new-modules-dynamically-in-run-time-with-angular-cli-angular-5/50395048#50395048 – Michael May 22 '18 at 07:01
  • @Seb How did you solve it in the end, without using SystemJS ? – vooxo Oct 06 '20 at 11:09
  • too long time to remember sry – Seb Oct 07 '20 at 12:04

2 Answers2

3

Yes, you need to add systemjs to your angular-cli and use it to load a module. Then you can use componentFactoryResolver to resolve the components you need in the module. To add systemjs to your project install it:

npm i systemjs

and the following into angular-cli.json:

"scripts": [
    "../node_modules/systemjs/dist/system.src.js"
],

Also add a link to scripts.js in the page :

This will load systemjs and it will available as a global object. You can then use it like this:

declare var SystemJS;

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor() {
    SystemJS.load(...);
  }

For details how to use SystemJS to load a module see How to load dynamic external components into Angular application answer

Seb
  • 3,602
  • 8
  • 36
  • 52
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
2

I have realized getting and loading remote component based on Angular 4. Feel free to check my github project:https://github.com/dianadujing/dynamic-remote-component-loader

DianaDu
  • 43
  • 5
  • Thanks for the sample! Would this work flawlessly with AOT? Maybe the JIT compiler has to be manually created and provided? – LppEdd Jan 28 '19 at 11:12
  • @LppEdd Actually, that's where I was stuck. Finally, I chose to upgrade my Angular version to 6. – DianaDu Jan 30 '19 at 03:36
  • Isn't it the same? I mean, when using AOT the JIT compiler gets stripped away from the page – LppEdd Jan 30 '19 at 08:13