如何在 single-spa 中跨不同的微应用程序共享数据

how to share data across different micro apps in single-spa

我了解如何在使用 single-spa 时将自定义道具传递给不同的微型应用程序,但我想知道如何获取 api 调用表单 app1 的输出并在 app2 中使用它。 app1 使用 react-redux 而 app2 将使用 react hooks.

import {registerApplication, start} from 'single-spa'


registerApplication(
    'navBar',          // Name of this single-spa application
    () => import('./nav/navbar.app.js').then(module => module.navBar),
    () => true
)

registerApplication(
  'app1',          // Name of this single-spa application
  () => import('./root.app.js'),//loadingFunction, // Our loading function
  () => true, //activityFunction // Our activity function
)

registerApplication(
    'app2',          // Name of this single-spa application
    () => import('./app2/app2.app.js').then(module => module.app2),
    location =>
        location.pathname === '/app2' ||
        location.pathname.startsWith('/app2'),
    {
        some: 'value',
    }
)

start()

或者我如何调用 root api 调用并在所有微应用程序之间共享输出。上面是我的 index.jswebpack.

调用

我正在使用单 spa 版本 4.x 和 webpack 4.x

Sharing application state in single-spa is achieved using a utility module pattern to create a "service" that can be leveraged using cross-microfrontend imports 分享你所有的微前端。

其中一个示例是使用 Rxjs 跨多个前端共享登录状态,您可以在此处找到:https://github.com/filoxo/single-spa-example-rxjs-shared-state免责声明:我编写了这个示例。