简体   繁体   English

如何在单独的存储库中与 2 个 Angular-Apps(微前端)共享服务实例?

[英]How to share an instance of a service with 2 Angular-Apps (Microfrontend) in separate repos?

I'm currently building an Intranet and I want to connect different applications (all Angular 14) to it.我目前正在构建一个 Intranet,我想将不同的应用程序(所有 Angular 14)连接到它。

One requirement is that the apps and the host should be in separated repos.一项要求是应用程序和主机应位于单独的存储库中。 Therefore I created 2 projects with nx and configured them, so that the apps are getting loaded dynamically.因此,我使用 nx 创建了 2 个项目并对其进行了配置,以便动态加载应用程序。 This part works pretty well.这部分工作得很好。

Now my problem is to share user-information between them (including events).现在我的问题是在他们之间共享用户信息(包括事件)。 Therefore I created a package, which all apps and the host referenced.因此,我创建了一个 package,所有应用程序和主机都引用了它。 The idea is that there is only one UserService for the whole application (including remotes).这个想法是整个应用程序(包括遥控器)只有一个 UserService。 But I get a new instance foreach remote.但是我为每个遥控器获得了一个新实例。

Is there a way to work with one instance throughout the whole environment, or do I have to stick with the sessionStorage?有没有办法在整个环境中使用一个实例,还是我必须坚持使用 sessionStorage?

You can take a look on provideIn: platform option on the service, but this depends on your structure.您可以查看服务上的provideIn: platform选项,但这取决于您的结构。 Otherwise you can create a mechanism of communication between your applications.否则,您可以创建应用程序之间的通信机制。 (there are a plenty of them with pro and cons, for example event mechanism ). (其中有很多优点和缺点,例如event mechanism )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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