lmeijdam / angular-umd-dynamic-example

Angular UMD example of loading new modules dynamically
174 stars 55 forks source link

Using services/modules from parent app #3

Open Disane87 opened 5 years ago

Disane87 commented 5 years ago

Hey, I realy love your approach which pushed me into the right direction for our plugin system. For that system, our custmers should be able to use a set of our services/modules/components to build up their own plugin.

Do you have any idea how we can provide a solution for that? Do we need to inject services and modules in the loadModule function? But how behave the intellisense within the plugin development when the modules and services of the root app are not known to the plugin?

yvz5 commented 5 years ago

for that I d use npm modules with private registry. but the real problem is the communication & dependency between modules

Disane87 commented 5 years ago

Yeah, but I guess this is not an option for us. We have several interceptors for http request for auth and getting the correct api url. Our customers shouldn‘t care about this, cause we have all of that in our main application

LeoLetourneur commented 5 years ago

I'm using libraries (Core and Shared) to provide some shared services and components. In my package.json I have: "dng-core": "file:./dist-lib/dng-core", "dng-shared": "file:./dist-lib/dng-shared",

I'm sharing these libraries throught systemjs and in my module I have: import { AlertService } from 'dng-shared';

And if an other person want to develop a module, I'll prodive core & shared libraries.

For http request/auth/interceptors, maybe you can provide some public methods but the tricky part could be private ?

yvz5 commented 5 years ago

I'm using libraries (Core and Shared) to provide some shared services and components. In my package.json I have: "dng-core": "file:./dist-lib/dng-core", "dng-shared": "file:./dist-lib/dng-shared",

I'm sharing these libraries throught systemjs and in my module I have: import { AlertService } from 'dng-shared';

And if an other person want to develop a module, I'll prodive core & shared libraries.

For http request/auth/interceptors, maybe you can provide some public methods but the tricky part could be private ?

i actually like this idea, got any sample code ?

Disane87 commented 5 years ago

The problem is that in the module.service.ts the modules are new initiated: https://github.com/lmeijdam/angular-umd-dynamic-example/blob/1999bcc7477e43f31e2f7961a7fcadaa51aadfb5/src/app/services/module.service.ts#L58

In this section I added the module @angular/http. But its a new instance of the http module and not the existing one and I don't know how to "add" the reference for the existing http module of my parent-app.

yvz5 commented 5 years ago

try this: SystemJS.set('@angular/http', SystemJS.newModule(this.http));

Disane87 commented 5 years ago

try this: SystemJS.set('@angular/http', SystemJS.newModule(this.http));

This look so right. It's the obvious thing to do, but I just didn't think of it. 🥇

yvz5 commented 5 years ago

try this: SystemJS.set('@angular/http', SystemJS.newModule(this.http));

This look so right. It's the obvious thing to do, but I just didn't think of it. 🥇

did it work ? i didnt try this yet

Disane87 commented 5 years ago

Gonna try it this week, actually I‘m pretty packed with other stuff. Gonna ping back when I‘ve got some results

yvz5 commented 5 years ago

you might wanna remove SystemJS.newModule as well, it looks like its creating a new instance. the correct way could be: SystemJS.set('@angular/http', this.http);

LeoLetourneur commented 5 years ago

I don't think that it is providing new instance. I'm using SystemJS.set(..., SystemJS.newModule(...)) with my shared and core modules. I have 3 load-on-fly modules (csv, nv, ov). Unless I'm mistaken, my core module which contains singleton shared service is initialized only once.

Short exemple:

I include CoreModule only in App, with shared services. I used the logger service to keep initialisation date (in logger service constructor : this.instance = new Date();)

I include SharedModule in all module (app, csv, nv). So it is initialized three times. But in shared module I have an AlertService which open toast. This is a singleton (used of forRoot(...)). Same things that the logger service for the initialization.

So first, I load csv module, and I print init date for Logger & Alert service in csv/home. Then I load nv module, and I print init date for logger & Alert service in vn/home. Finally I go back to csv/home.

capture

As you can see, dates are the same for csv and nv module. So I think that even if we use 'SystemJS.newModule'. Instances are the same.

Am I missing something or am I wrong ?

i actually like this idea, got any sample code ?

@yvz5 : Sample code not for today, but I'll try to extract from my current project.

GeorgesSaad commented 4 years ago

Anyone figured out how to use HttpClient in a module component?