简体   繁体   English

激活路由时无法加载角度联合模块

[英]Angular federated module can not be loaded when route would be activated

I am trying to achieve a very simple module federation with shell (host) and travel (remote).我正在尝试使用 shell(主机)和 travel(远程)实现一个非常简单的模块联合。 Whenever I try do dynamically load the AbcModule from travel I get the following error in the shell:每当我尝试从 travel 中动态加载 AbcModule 时,我都会在 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)错误错误:未捕获(承诺中):TypeError:无法读取未定义的属性(读取'init')TypeError:无法在angular-architects-module-federation-runtime.js:26读取未定义的属性(读取'init'): 1 在 Generator.next () 在 _ZoneDelegate.invoke (zone.js:372:1) 在 Object.onInvoke (core.mjs:25634:33) 在 _ZoneDelegate.invoke (zone.js:371:1) 在 Zone.run (zone.js:134:1) 在 zone.js:1275:1 在 _ZoneDelegate.invokeTask (zone.js:406:1) 在 Object.onInvokeTask (核心。 mjs:25621:33) 在 resolvePromise (zone.js:1211:1) 在 zone.js: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) 在 Zone.run (zone.js:134:1) 在 zone.js:1275:1 在 _ZoneDelegate .invokeTask (zone.js:406:1) 在 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错误错误:未捕获(承诺中):TypeError:无法读取未定义的属性(读取'get')TypeError:无法在angular-architects-module-federation-runtime.js:9读取未定义的属性(读取'get'): 1 在 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) 在 angular-architects-module-federation-runtime.js:106:1 在 Generator.next () 在完成 (tslib.es6.js:73:43) 在 _ZoneDelegate.invoke (zone.js:372:1) 在 resolvePromise (zone.js:1211:1) 在 resolvePromise (zone.js:1165:1) 在 zone.js: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 (zone.js:585: 1) 在 ZoneTask.invokeTask [作为调用] (zone.js:49

app routes from shell:来自 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:来自 shell 的 webpack.config.js:

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:旅行中的 webpack.config.js:

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:旅行中的 AbcModule:

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 got it working by changing the loadRemoteModule parameter to this:我通过将 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)
  }
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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