简体   繁体   English

如何通过 Angular 模块联合访问共享资产?

[英]How do I access a shared asset via Angular mdule federation?

I'm using Angular 14 and module federation.我正在使用 Angular 14 和模块联合。 In my remote app, I have this file structure在我的远程应用程序中,我有这个文件结构

- package.json
- webpack.config.js
+ src
    - settings.json
    + app
        + services
            - app.service.ts

In my app.service.ts file, I access a static JSON file like so在我的 app.service.ts 文件中,我像这样访问一个静态 JSON 文件

@Injectable({
  providedIn: 'root'
})
export class AppService {
    ...
     public init() {
        const request = new XMLHttpRequest();
        request.open('GET', this.configUrl, false);
        request.send(null);
        ...
    }

In my webpack.config.js file I attempt to expose my module and file like so在我的 webpack.config.js 文件中,我尝试像这样公开我的模块和文件

module.exports = withModuleFederationPlugin({

  name: 'productlist',

  exposes: {
    './Component': './src/app/app.component.ts',
    './dashboard':'./src/app/my-product-list/my-product-list.module.ts'
  },

  shared: {
    '.settings.json': {
      singleton: true,
      eager: true,
      import: './src/settings.json',
      requiredVersion: 'auto',
    },
    ...shareAll({ singleton: true, strictVersion: true, requiredVersion: 'auto' }),
  },

});

Problem is, when I access my remote via a shell application, the "settings.json" file can no longer be found.问题是,当我通过 shell 应用程序访问我的遥控器时,无法再找到“settings.json”文件。 How do I reference it from, or share it with my shell?我如何从我的 shell 中引用它或与它共享它? I have this in my shell's webpack.config.js file我的 shell 的 webpack.config.js 文件中有这个

module.exports = withModuleFederationPlugin({

  name: 'shellapp',

  remotes: {
    "productlist": "http://localhost:4204/remoteEntry.js",
    "settings.json": "http://localhost:4202/settings.json",
  },
    ...

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

});

to share the settings.json file with your shell application you will need to include it in the shared section of your shell's webpack.config.js file.要与您的 shell 应用程序共享settings.json文件,您需要将其包含在 shell 的webpack.config.js文件的共享部分中。 in the shared sectin you can specify the settings.json file as follows:shared部分中,您可以按如下方式指定settings.json文件:

shared: {
    'settings.json': {
      singleton: true,
      eager: true,
      import: 'http://localhost:4202/settings.json',
      requiredVersion: 'auto',
    },
    ...shareAll({ singleton: true, strictVersion: true, requiredVersion: 'auto' }),
  },

this will make the settings.json file available to your shell application and allow you to import it using the import statement in your shell's code as follows:这将使settings.json文件可用于您的 shell 应用程序,并允许您使用 shell 代码中的import语句导入它,如下所示:

import settings from 'settings.json';

Updated answer:更新的答案:

I made some research.我做了一些研究。 I think, is not possible to share settings.json through the webpack.config.js file.我认为,不可能通过webpack.config.js文件共享settings.json A better approach would be to create a settings library that loads the settings from the settings.json file and exposes them through interfaces or services.更好的方法是创建一个设置库,从 settings.json 文件加载设置并通过接口或服务公开它们。

The only way I found so far was this article到目前为止我找到的唯一方法是这篇文章


I got it wrong thats why I updated my answer, I think you should rename .settings.json to settings.json我弄错了,这就是我更新答案的原因,我认为您应该将.settings.json重命名为settings.json

module.exports = withModuleFederationPlugin({

  shared: {
    'settings.json': {
      singleton: true,
      eager: true,
      import: './src/settings.json',
      requiredVersion: 'auto',
    }
  },
});

I think you should reference the settings.json file from your shell application using module federation in Angular.我认为您应该使用 Angular 中的模块联合从您的 shell 应用程序中引用settings.json文件。 So you also expose the file to the shell application in the exposess property of the remote application's webpack.config.js file, like this..因此,您还在远程应用程序的webpack.config.js文件的exposess属性中将该文件公开给 shell 应用程序,如下所示。

In the remote application's webpack.config.js file:在远程应用程序的webpack.config.js文件中:

module.exports = withModuleFederationPlugin({
  name: 'productlist',

  exposes: {
    './settings.json': './src/settings.json',
  },

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

In the shell application's webpack.config.js file:在 shell 应用程序的webpack.config.js文件中:

module.exports = withModuleFederationPlugin({
  name: 'fiduciary',

  remotes: {
    "productlist": "http://localhost:4204/remoteEntry.js",
  },

  shared: {
    "settings.json": {
      requiredVersion: 'auto',
      remote: "productlist/settings.json",
    },
    ...shareAll({ singleton: true, strictVersion: true, requiredVersion: 'auto' }),
  },
});

Then, you should be able access the settings.json file in your shell application:然后,您应该能够在您的 shell 应用程序中访问settings.json文件:

import settings from 'settings.json';

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

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