提问者:小点点

当路由被激活时,角联邦模块无法加载


我正在尝试使用shell(主机)和Travel(远程)实现一个非常简单的模块联合。每当我尝试从Travel动态加载AbcModule时,shell中都会出现以下错误:

错误:未捕获(在promise中):类型错误:无法读取未定义的属性(读取'init')类型错误:无法读取未定义的属性(读取'init')在angel-Architects-module-fainity-runtime. js:26:1在Generator.next()在履行(tslb.es6.js:73:43)在_ZoneDelegate.invoke(zone.js:372:1)在Object.onInvoke(core.mjs:25634:33)在_ZoneDelegate调用(zone.js:371:1)在Zone.run(zone.js:134:1)在zone.js:1275:1在_ZoneDelegate调用任务(zone.js:406:1)在Object.onInvokeTask(core.mjs:25621:33)在resvePromise(zone.js:1211:1)在zone.js:1118:1在履行(tslb.es6.js:73zone. js:1275:1 at_ZoneDelegate.callkeTask(zone.js:406:1)at Object.onInvokeTask(core.mjs:25621:33)

错误:未捕获(在promise中):类型错误:无法读取未定义的属性(读取'get')类型错误:无法读取未定义的属性(读取'get')在angel-Archittes-module-runtime. js:9:1在Generator.next()在tslb.es6.js:76:1在新ZoneAware Promise(zone.js:1427:1)在__awaiter(tslb.es6.js:72:1)在lookupExposedModule(angel-Archittes-module-runtime.js:7:21)在angel-Archittes-module-fetial-runtime.js:106:1在Generator.next()在履行(tslb.es6.js:73:43)在_ZoneDelegate.invoke(zone.js:372:1)在resvePromise(zone.js:1211:1)在resvePromise(zone.js:1165:1)在zone.js:1278:1在Zone. runTask(zone.js:178:1)at drainMicroTaskQueue(zone.js:585:1)at ZoneTask.callkeTask[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)
  }
];

来自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:

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:

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 {}

共1个答案

匿名用户

我通过将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)
  }
];

相关问题