angular-architects / module-federation-plugin

MIT License
683 stars 184 forks source link

Angular + web components + shared service #534

Closed zerock54 closed 2 weeks ago

zerock54 commented 1 month ago

Hello,

Here's my current setup:

As my shell and remote do not use the same Angular version, i had, if I have correctly understood the documentation, wrap my remote into a web component.

Now I'd like to make the shell and the remote use the same instance of my Angular lib. I added it to my shared libs like this:

(Shell)

image

(Remote)

image

I also set "providedIn: root" to the shared service.

It still doesn't seem to work as I can see the service's constructor called twice when I load the page.

So my question is: is it even feasible ? Sharing an Angular service when the remote is a web component ? Does it only work if the remote and the shell share the same version and therefore the remote can "simply" be lazy-loaded like any other Angular module ?

Thanks

manfredsteyer commented 2 weeks ago

In this scenario, you cannot share a service because each Angular app has its own injector tree. But you could share a plain javascript object, sth like

export const sharedObject = { x: 1, y: 2 };