首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在模块联邦体系结构中调用两次RouterModule.forRoot()

在模块联邦体系结构中调用两次RouterModule.forRoot()
EN

Stack Overflow用户
提问于 2022-03-02 16:58:22
回答 1查看 561关注 0票数 0

ERROR Error: Uncaught (in promise): Error: RouterModule.forRoot() called twice. Lazy loaded modules should use RouterModule.forChild() instead.

问题:我使用的是"webpack":"5.67.0“和”@角-架构师/模块-联邦“:"^14.1.1",我的主机应用程序和远程应用程序都是独立工作的。但是当我尝试查看shell内部的远程应用程序时,它给了我从上面的错误。当我将forChild()添加到远程时,我的样式和图像并不来自远程,它们只来自主机。我做错了什么?

来自主机的Webpack配置:

代码语言:javascript
复制
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
const mf = require("@angular-architects/module-federation/webpack");
const path = require("path");
const share = mf.share;

const sharedMappings = new mf.SharedMappings();
sharedMappings.register(
  path.join(__dirname, 'tsconfig.json'),
  [/* mapped paths to share */]);

module.exports = {
  output: {
    uniqueName: "container",
    publicPath: "auto"
  },
  optimization: {
    runtimeChunk: false
  },   
  resolve: {
    alias: {
      ...sharedMappings.getAliases(),
    }
  },
  experiments: {
    outputModule: true
  },
  plugins: [
    new ModuleFederationPlugin({
        library: { type: "module" },

        // For remotes (please adjust)
        // name: "container",
        // filename: "remoteEntry.js",
        // exposes: {
        //     './Component': './/src/app/app.component.ts',
        // },        
        
        // For hosts (please adjust)
        remotes: {
          "web": "http://localhost:8081/remoteEntry.js",
        },

        shared: share({
          "@angular/core": { singleton: true, strictVersion: true, eager: true, requiredVersion: '>= 13.2.4' }, 
          "@angular/common": { singleton: true, strictVersion: true, eager: true, requiredVersion: '>= 13.2.4' }, 
          "@angular/common/http": { singleton: true, strictVersion: true, eager: true, requiredVersion: '>= 13.2.4' }, 
          "@angular/router": { singleton: true, strictVersion: true, eager: true, requiredVersion: '>= 13.2.4' },

          ...sharedMappings.getDescriptors()
        })
        
    }),
    sharedMappings.getPlugin()
  ],
};

Webpack配置来自远程:

代码语言:javascript
复制
const ModuleFederationPlugin = require("webpack/lib/container/ModuleFederationPlugin");
const mf = require("@angular-architects/module-federation/webpack");
const path = require("path");

const share = mf.share
const sharedMappings = new mf.SharedMappings();
sharedMappings.register(
  path.join(__dirname, 'tsconfig.json'),
  ['home']);

module.exports = {
  output: {
    uniqueName: "web",
    publicPath: "auto"
  },
  optimization: {
    runtimeChunk: false
  },   
  resolve: {
    alias: {
      ...sharedMappings.getAliases(),
    }
  },
  experiments: {
    outputModule: true
  },
  module: {
    rules: [
        {
            test: /\.svg$/,
            use: [
              {
                loader: 'svg-url-loader',
                options: {
                  limit: 10000,
                },
              },
            ],
        },
        {
            test: /\.jpe?g$|\.ico$|\.gif$|\.png$|\.svg$|\.woff$|\.ttf$|\.wav$|\.mp3$|\.css/,
            use: {
                loader: 'file-loader?name=[name].[ext]'
            }
        }
    ]
  },
  plugins: [
    new ModuleFederationPlugin({
        library: { type: "module" },
        name: "web",
        filename: "remoteEntry.js",
        exposes: {
          './WebsiteModule': './src/app/app.module.ts',
        },
        shared: 
        share({
          "@angular/core": { singleton: true, strictVersion: true, eager: true, requiredVersion: '>= 13.2.4', }, 
          "@angular/common": { singleton: true, strictVersion: true, eager: true, requiredVersion: '>= 13.2.4', }, 
          "@angular/common/http": { singleton: true, strictVersion: true, eager: true, requiredVersion: '>= 13.2.4', }, 
          "@angular/router": { singleton: true, strictVersion: true, eager: true, requiredVersion: '>= 13.2.4', },
          "@angular/material": { singleton: true, strictVersion: true, eager: true },
          "@angular/cdk": { singleton: true, strictVersion: true, eager: true },
          ...sharedMappings.getDescriptors()
        })
        
    }),
    sharedMappings.getPlugin()
  ],
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-07 01:45:42

我相信您可以更改为微前端服务的url的汽车公共路径,例如

代码语言:javascript
复制
publicPath: "auto"

代码语言:javascript
复制
publicPath: "localhost:5000"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71326434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档