在使用共享模块的应用程序中,需要将共享模块设置为懒加载模式。假设我们有一个实现共享模块的Angular项目,称为“shared”,并有一个使用该共享模块的主应用程序,称为“main”。我们需要进行以下更改:
@NgModule({
imports: [],
exports: [],
declarations: [],
providers: [],
bootstrap: [],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
// 加载方式设置为懒加载模式
// (当使用 webpack5 时配置文件webpack.config.js中,需要设置module.federation.shared.type为'lazy')
// (当使用 webpack5 时,需要设置export来使模块可被引用)
// (当使用 webpack4 时,需要将entry中对应模块设置为输出文件,然后在主模块使用script引入)
...(isWebpack5 ?
{ import: [''], type: 'lazy', shareScope: 'default', export: 'sharedModule' } :
{ },
)
export class AppModule { }
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
// 导入共享模块
// (当使用 webpack5 时配置文件webpack.config.js中,需要设置module.federation.shared.type为'lazy')
// (当使用 webpack5 时,需要设置import来引用模块)
// (当使用 webpack4 时,需要将entry中对应模块设置为输出文件,然后在主模块使用script引入)
...(isWebpack5 ? [
loadRemoteModule({
remoteEntry: 'http://localhost:5000/remoteEntry.js',
remoteName: 'shared',
exposedModule: './sharedModule'