当路线被激活时,角联合模块无法加载

发布于 2025-01-28 15:10:59 字数 5311 浏览 4 评论 0原文

我正在尝试与Shell(主机)和Travel(远程)实现非常简单的模块联合会。每当我尝试动态加载旅行中的AbcModule时,我会在Shell中获得以下错误:

错误错误:未被发达(在承诺中):TypeError:无法读取属性 未定义(读取'init')typeError:无法读取的属性 未定义(阅读“初始化”) 在Angular-Architects-Module-FeDeration-Runtime.js:26:1 在genertator.next() 在满足(TSLIB.ES6.JS:73:43) 在_zonedelegate.invoke(Zone.JS:372:1) 在object.oninvoke(core.mjs:25634:33) 在_zonedelegate.Invoke(Zone.JS:371:1) 在区域。 在区域:1275:1 在_zonedelegate.invoketask(Zone.JS:406:1) 在object.oninvoketask(core.mjs:25621:33) 在Resolve Promise(Zone.JS:1211:1) 在区域:1118:1 在满足(TSLIB.ES6.JS:73:86) 在_zonedelegate.invoke(Zone.JS:372:1) 在object.oninvoke(core.mjs:25634:33) 在_zonedelegate.Invoke(Zone.JS:371:1) 在区域。 在区域:1275:1 在_zonedelegate.invoketask(Zone.JS:406:1) 在object.oninvoketask(core.mjs:25621:33)

错误错误:未被发达(在承诺中):TypeError:无法读取属性 未定义(读取'get')typeError:无法读取的属性 未定义(阅读“获取”) 在Angular-Architects-Module-FeDeration-Runtime.js:9:1 在genertator.next() 在tslib.es6.js:76:1 在新的ZoneAwarePromise(Zone.JS:1427:1) 在__awaiter(tslib.es6.js:72:1) 在LookupExpedModule(Angular-Architects-Module-FeDeration-runtime.js:7:21) 在Angular-Architects-Module-FeDeration-Runtime.js:106:1 在genertator.next() 在满足(TSLIB.ES6.JS:73:43) 在_zonedelegate.invoke(Zone.JS:372:1) 在Resolve Promise(Zone.JS:1211:1) 在Resolve Promise(Zone.JS:1165:1) 在区域:1278:1 在_zonedelegate.invoketask(Zone.JS:406:1) 在object.oninvoketask(core.mjs:25621:33) 在_zonedelegate.invoketask(Zone.JS:405:1) 在Zone.Runtask(Zone.JS:178:1) 在drainmicrotaskqueue(区域:585:1) 在Zonetask.invoketask [AS Invoke](Zone.JS:49

来自Shell的应用程序路由:

import { loadRemoteModule } from "@angular-architects/module-federation";
import { Routes } from "@angular/router";
import { TestComponent } from "./test/test.component";

export const APP_ROUTES: Routes = [
  {
    path: '',
    component: TestComponent,
    pathMatch: 'full'
  },
  {
    path: 'abc',
    loadChildren: () => loadRemoteModule({
      remoteEntry: 'http://localhost:4201/remoteEntry.js',
      remoteName: 'travel',
      exposedModule: './Module'
    })
    .then(m => m.AbcModule)
  }
];

webpack.config.js来自Shell:

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.base.json'),
  [/* mapped paths to share */]);

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


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

          ...sharedMappings.getDescriptors()
        })

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

Webpack.config.js from Travel:

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.base.json'),
  [/* mapped paths to share */]);

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

       name: "travel",
       filename: "remoteEntry.js",
       exposes: {
           './Module': './apps/travel/src/app/abc/abc.module.ts',
       },

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

          ...sharedMappings.getDescriptors()
        })

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

abcmodule:travel:travel:travel:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AbcComponent } from './abc/abc.component';
import { RouterModule } from '@angular/router';

@NgModule({
  declarations: [AbcComponent],
  imports: [CommonModule, RouterModule.forChild([
    {
      path: '',
      component: AbcComponent
    }
  ])],
  exports: [RouterModule]
})
export class AbcModule {}

I am trying to achieve a very simple module federation with shell (host) and travel (remote). Whenever I try do dynamically load the AbcModule from travel I get the following error in the shell:

ERROR Error: Uncaught (in promise): TypeError: Cannot read properties
of undefined (reading 'init') TypeError: Cannot read properties of
undefined (reading 'init')
at angular-architects-module-federation-runtime.js:26:1
at Generator.next ()
at fulfilled (tslib.es6.js:73:43)
at _ZoneDelegate.invoke (zone.js:372:1)
at Object.onInvoke (core.mjs:25634:33)
at _ZoneDelegate.invoke (zone.js:371:1)
at Zone.run (zone.js:134:1)
at zone.js:1275:1
at _ZoneDelegate.invokeTask (zone.js:406:1)
at Object.onInvokeTask (core.mjs:25621:33)
at resolvePromise (zone.js:1211:1)
at zone.js:1118:1
at fulfilled (tslib.es6.js:73:86)
at _ZoneDelegate.invoke (zone.js:372:1)
at Object.onInvoke (core.mjs:25634:33)
at _ZoneDelegate.invoke (zone.js:371:1)
at Zone.run (zone.js:134:1)
at zone.js:1275:1
at _ZoneDelegate.invokeTask (zone.js:406:1)
at Object.onInvokeTask (core.mjs:25621:33)

ERROR Error: Uncaught (in promise): TypeError: Cannot read properties
of undefined (reading 'get') TypeError: Cannot read properties of
undefined (reading 'get')
at angular-architects-module-federation-runtime.js:9:1
at Generator.next ()
at tslib.es6.js:76:1
at new ZoneAwarePromise (zone.js:1427:1)
at __awaiter (tslib.es6.js:72:1)
at lookupExposedModule (angular-architects-module-federation-runtime.js:7:21)
at angular-architects-module-federation-runtime.js:106:1
at Generator.next ()
at fulfilled (tslib.es6.js:73:43)
at _ZoneDelegate.invoke (zone.js:372:1)
at resolvePromise (zone.js:1211:1)
at resolvePromise (zone.js:1165:1)
at zone.js:1278:1
at _ZoneDelegate.invokeTask (zone.js:406:1)
at Object.onInvokeTask (core.mjs:25621:33)
at _ZoneDelegate.invokeTask (zone.js:405:1)
at Zone.runTask (zone.js:178:1)
at drainMicroTaskQueue (zone.js:585:1)
at ZoneTask.invokeTask [as invoke] (zone.js:49

app routes from shell:

import { loadRemoteModule } from "@angular-architects/module-federation";
import { Routes } from "@angular/router";
import { TestComponent } from "./test/test.component";

export const APP_ROUTES: Routes = [
  {
    path: '',
    component: TestComponent,
    pathMatch: 'full'
  },
  {
    path: 'abc',
    loadChildren: () => loadRemoteModule({
      remoteEntry: 'http://localhost:4201/remoteEntry.js',
      remoteName: 'travel',
      exposedModule: './Module'
    })
    .then(m => m.AbcModule)
  }
];

webpack.config.js from shell:

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.base.json'),
  [/* mapped paths to share */]);

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


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

          ...sharedMappings.getDescriptors()
        })

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

webpack.config.js from travel:

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.base.json'),
  [/* mapped paths to share */]);

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

       name: "travel",
       filename: "remoteEntry.js",
       exposes: {
           './Module': './apps/travel/src/app/abc/abc.module.ts',
       },

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

          ...sharedMappings.getDescriptors()
        })

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

AbcModule from travel:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AbcComponent } from './abc/abc.component';
import { RouterModule } from '@angular/router';

@NgModule({
  declarations: [AbcComponent],
  imports: [CommonModule, RouterModule.forChild([
    {
      path: '',
      component: AbcComponent
    }
  ])],
  exports: [RouterModule]
})
export class AbcModule {}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眸中客 2025-02-04 15:10:59

我通过更改loadRemoteModule参数来使它起作用:

import { loadRemoteModule } from "@angular-architects/module-federation";
import { Routes } from "@angular/router";
import { TestComponent } from "./test/test.component";

export const APP_ROUTES: Routes = [
  {
    path: '',
    component: TestComponent,
    pathMatch: 'full'
  },
  {
    path: 'abc',
    loadChildren: () => loadRemoteModule({
      remoteEntry: 'http://localhost:4201/remoteEntry.js',
      type: 'module',
      exposedModule: './Module'
    })
    .then(m => m.AbcModule)
  }
];

I got it working by changing the loadRemoteModule parameter to this:

import { loadRemoteModule } from "@angular-architects/module-federation";
import { Routes } from "@angular/router";
import { TestComponent } from "./test/test.component";

export const APP_ROUTES: Routes = [
  {
    path: '',
    component: TestComponent,
    pathMatch: 'full'
  },
  {
    path: 'abc',
    loadChildren: () => loadRemoteModule({
      remoteEntry: 'http://localhost:4201/remoteEntry.js',
      type: 'module',
      exposedModule: './Module'
    })
    .then(m => m.AbcModule)
  }
];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文