单 SPA 本地主机 CROS 起源问题 - React 应用程序

shr*_*aju 6 reactjs systemjs single-spa

我是反应新手..我有反应应用程序,我已将其迁移到 Single-SPA。为了将我的应用程序与主应用程序(具有许多 Vue single-spa 应用程序的平台)集成,我将其重新构造为

MainApp - 它包括与进程相关的所有页面 - 在 localhost:3000 下运行

root-html-file - 它包括一个index.html和package.json文件index.html(在localhost:5000下运行-由SPA生成)

代码: root-html-file -> index.html

<script type="systemjs-importmap">
      {
        "imports": {
    "myapp": "http://localhost:3000/",
    "single-spa": "https://cdnjs.cloudflare.com/ajax/libs/single-spa/4.3.7/system/single-spa.min.js",
    "react": "https://cdnjs.cloudflare.com/ajax/libs/react/16.8.6/umd/react.development.js"
     }}</script>

Promise.all([System.import('single-spa'),System.import('react')]).then(function (modules) {
          var singleSpa = modules[0];
            singleSpa.registerApplication(
                'myapp',
                () => System.import('myapp'),
                location => true
            );
          singleSpa.start();
Run Code Online (Sandbox Code Playgroud)

myapp 应用程序作为独立的工作正常。仅当运行 root-html-file 应用程序(npm runserve)时,该应用程序会将 myapp 应用程序加载到其中,从而导致 CROS origin 问题。请查看屏幕截图

在此输入图像描述

如果我方向错误,请指导我。

Gar*_*nca 2

您需要将访问控制允许来源标头添加到 React 应用程序。在 webpack 中,可以通过将以下内容添加到 webpack.config.js 文件中来完成

devServer: {
  headers: {
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, PATCH, OPTIONS",
    "Access-Control-Allow-Headers": "X-Requested-With, content-type, Authorization"
  }
}
Run Code Online (Sandbox Code Playgroud)