简体   繁体   中英

RouterModule.forRoot() called twice in a Module Federation architecture

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

Question: I am using "webpack": "5.67.0" and "@angular-architects/module-federation": "^14.1.1", and I have both the host app and remote app working in isolation. But when I try the view the remote app inside of the shell it is giving me that error from above. When I do add the forChild() to the remote my styles and images don't come from the remote they come from the host only. What am I doing wrong?

Webpack config from host:

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 config from remote:

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()
  ],
};

I believe you can change o public path of auto for the url that is serving the microfrontend, example

publicPath: "auto"

to

publicPath: "localhost:5000"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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