简体   繁体   English

Angular 14、使用模块联合,我如何从远程应用程序内部确定我的远程应用程序的绝对路径?

[英]In Angular 14, using module federation, how do I determine the absolute path of my remote app from within the remote app?

I'm using Angular 14 and module federation.我正在使用 Angular 14 和模块联合。 How do I find the absolute path of my remote application from within my remote application?如何从我的远程应用程序中找到我的远程应用程序的绝对路径? In my remote application, I expose the module using在我的远程应用程序中,我使用公开模块

module.exports = withModuleFederationPlugin({

  name: 'checklogo',

  exposes: {
    './Component': './src/app/app.component.ts',
    './start':'./src/app/my-products/my-products.module.ts'
  },

  shared: {
    ...shareAll({ singleton: true, strictVersion: true, requiredVersion: 'auto' }),
  },

});

and then in my src/app/services I have this然后在我的 src/app/services 我有这个

@Injectable({
  providedIn: 'root'
})
export class SettingsService {

    ...
     public init() {
        const absolutePath = ???
        this.configuration = initFile(`${absolutePath}/config.json`);

In my shell application, I reference the remote module when I init my routes like so在我的 shell 应用程序中,我在初始化路由时引用了远程模块

const routes: Routes = [
  {
    ...
      {
        path: 'my-products',
        initChildren: () =>
            initRemoteModule({
                type: 'module',
                remoteEntry: getRemoteEntryUrl(),
                exposedModule: './start'
            })
            .then(m => m.MyProductsModule)
      },

I don't quite know what to put in the "const absolutePath =???"我不太清楚要在“const absolutePath =???”中放入什么? line of my "init" method within the service.服务中我的“init”方法的行。

To achieve that, the remote entries should be defined in the shell app (not in the remote one) either in a static way or dynamically.为此,应以 static 方式或动态方式在shell应用程序(而不是远程应用程序)中定义remote entries

Static Federation approach: Static 联邦方式:

// projects\shell\webpack.config.js

module.exports = withModuleFederationPlugin({
  remotes: {
    mfe1: 'http://localhost:4201/remoteEntry.js',
  },
  //...
}

Dynamic Federation approach:动态联合方法:

Adjust the shell's main.ts (projects/shell/src/main.ts) as follows:调整shell的main.ts(projects/shell/src/main.ts)如下:

import { loadManifest } from '@angular-architects/module-federation';

// You can use a JSON manifest from assets or from server: 
loadManifest('assets/mf.manifest.json')
  .catch((err) => console.error('Error loading remote entries', err))
  .then(() => import('./bootstrap'))
  .catch((err) => console.error(err));

The JSON manifest content should be as follows: JSON清单内容应该如下:

{
  "mfe1": "http://localhost:4201/remoteEntry.js"
}

For more details about Webpack Module Federation in Angular, see: https://github.com/angular-architects/module-federation-plugin/blob/main/libs/mf/tutorial/tutorial.md Angular中关于Webpack Module Federation的详细介绍参见: https://github.com/angular-architects/module-federation-plugin/blob/main/libs/mf/tutorial/tutorial.md

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

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