我有一个shell 和两个使用NX模块联盟的MFE,即

  • agency(a)
  • home(b).

它托管在不同的子域中.

我在try 访问时遇到问题.

我的module-federation.manifest.json

{
  "agency": "https://a.abc.maindomain.com",
  "home": "https://b.abc.maindomain.com",
}

main.ts

import { setRemoteDefinitions } from '@nrwl/angular/mf';

fetch('/assets/module-federation.manifest.json')
  .then((res) => res.json())
  .then((definitions) => setRemoteDefinitions(definitions))
  .then(() => import('./bootstrap').catch((err) => console.error(err)));

以下是我的控制台错误

enter image description here

请指出我在这里做错了什么.

推荐答案

即使您要部署到子域中,它也会被视为2个域,因此您需要设置CORS启用码.如果您使用IIS进行部署,请执行以下步骤.

第一步:-FOR CORS

在此图像中,您可以看到HTTP响应头,为shell 和远程添加‘Access-Control-Allow-Origin’:‘*’.

IIS HTTP response header here

如果您有‘Web.config’,您可以在该文件中给出值.

 <system.webServer>
         <httpProtocol>
             <customHeaders>
                <add name="Access-Control-Allow-Origin" value="*" />
                <add name="Access-Control-Allow-Methods" value="*" />
                <add name="Access-Control-Allow-Headers" value="*" />
             </customHeaders>
         </httpProtocol>
    </system.webServer>

第二步:-ERR_FAILED 404

在您的屏幕截图中,emoteEntry扩展名不是.js,而是.mjs,所以可能服务器没有这个扩展名,所以只需在服务器MIME类型中添加这个扩展名(对于shell 和远程).请参阅下图

IIS mime types here

或者,您也可以将此配置添加到web.config中

<configuration>
   <system.webServer>
      <staticContent>
         <mimeMap fileExtension=".mjs" mimeType="text/javascript" />      
      </staticContent>
   </system.webServer>
</configuration>

第三步:-Restart the server.

试试这个,对我来说很管用.

Angular相关问答推荐

无法绑定到ngIf,因为它不是dis angular 17的已知属性

访问Angular 模板中的HTML元素属性

未触发HTTP拦截器

Angular react 形式验证问题

URL 更改但组件未在 Angular 导航中呈现

PrimeNG 避免在同一位置使用不同键的 cogo toast 重叠

如果Angular 数据为空,如何设置N/A

Storybook 和 Angular 交互游戏测试未定义预期

Subject.complete() 是否取消订阅所有听众?

Observable .do() 运算符 (rxjs) 的用例

无法卸载 angular-cli

如何在angular 5 中获取基本网址?

Angularmaterial步进器:禁用标题导航

angular 2 http withCredentials

Angular 4 错误:没有 HttpClient 的provider提供者

Angular 2 单元测试 - 出现错误无法加载ng:///DynamicTestModule/module.ngfactory.js

在 Angular 2 中使用逗号作为列表分隔符

如何在 Angular Material 中设置图标的 colored颜色 ?

IE11中的Angular4应用程序运行问题

如何将 @Input 与使用 ComponentFactoryResolver 创建的组件一起使用?