我正在尝试建立一个POC的微前端使用角和Webpack模块联合会。在这个过程中,我创建了一个shell应用程序和另一个mfe1应用程序,并在一个特定的路径上呈现mfe1,它正在按预期工作,并呈现该应用程序。现在,我正在尝试创建另一个名为mfe2的应用程序并呈现它。在这个mfe2中,我使用角元素创建了一个web组件,并在shell应用程序中进行渲染。当我这样做时,我将面临以下问题
错误:已经创建了一个配置不同的平台。请先销毁它。
当以下代码正在执行时
import('mfe2/web-component')
.then(_ => console.debug(`element loaded!`))
.catch(err => console.error(`error loading `, err));我不明白到底是怎么回事。我在下面添加所需的代码。
MFE2:
app.module.ts
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule
],
providers: [],
bootstrap: []
})
export class AppModule {
constructor(private injector: Injector) {}
ngDoBootstrap() {
const ce = createCustomElement(AppComponent, {injector: this.injector});
customElements.define('mfe2-elem', ce);
}
}webpack.config.js
module.exports = {
output: {
uniqueName: "mfe2",
publicPath: "auto"
},
optimization: {
runtimeChunk: false
},
resolve: {
alias: {
...sharedMappings.getAliases(),
}
},
plugins: [
new ModuleFederationPlugin({
// For remotes (please adjust)
name: "mfe2",
filename: "remoteEntry.js",
exposes: {
'./web-component': './src/bootstrap.ts',
},
// For hosts (please adjust)
// remotes: {
// "mfe1": "mfe1@http://localhost:3000/remoteEntry.js",
// },
shared: {
"@angular/core": { singleton: true, strictVersion: true },
"@angular/common": { singleton: true, strictVersion: true },
"@angular/common/http": { singleton: true, strictVersion: true },
"@angular/router": { singleton: true, strictVersion: true },
...sharedMappings.getDescriptors()
}
}),
sharedMappings.getPlugin()
],
};外壳:
在组件中呈现它:
@Component({
selector: 'app-mfe2element',
templateUrl: './mfe2element.component.html',
styleUrls: ['./mfe2element.component.scss']
})
export class Mfe2elementComponent implements AfterContentInit {
@ViewChild('vc', { read: ElementRef, static: true })
vc!: ElementRef;
constructor() { }
ngAfterContentInit(): void {
import('mfe2/web-component')
.then(_ => console.debug(`element loaded!`))
.catch(err => console.error(`error loading `, err));
// await import('mfe1/web-components');
// const element = document.createElement('mfe1-element');
// document.body.appendChild(element);
const element = document.createElement('mfe2-elem');
this.vc.nativeElement.appendChild(element);
}
}webpack.config.js
module.exports = {
output: {
uniqueName: "shell"
},
optimization: {
// Only needed to bypass a temporary bug
runtimeChunk: false
},
plugins: [
new ModuleFederationPlugin({
// For remotes (please adjust)
// name: "shell",
// filename: "remoteEntry.js",
// exposes: {
// './Component': './/src/app/app.component.ts',
// },
// For hosts (please adjust)
remotes: {
"mfe1": "mfe1@http://localhost:3000/remoteEntry.js",
"mfe2": "mfe2@http://localhost:4000/remoteEntry.js",
},
shared: {
"@angular/core": { singleton: true, strictVersion: false },
"@angular/common": { singleton: true, strictVersion: false },
"@angular/router": { singleton: true, strictVersion: false },
...sharedMappings.getDescriptors()
}
}),
sharedMappings.getPlugin(),
],
};请帮我解决这个问题。
谢谢..。
发布于 2021-05-24 06:28:44
从这里获得解决方案:https://github.com/angular-architects/module-federation-plugin/issues/47#issuecomment-845145887
根据共享的角度版本,我们只允许创建一个平台。为了记住我们的版本已经有了一个共享的平台,我们可以将它放入一个将版本号映射到平台实例的全局字典中:
const ngVersion = require('../package.json').dependencies['@angular/core']; // better just take the major version
(window as any).plattform = (window as any).plattform || {};
let platform = (window as any).plattform[ngVersion];
if (!platform) {
platform = platformBrowser();
(window as any).plattform[ngVersion] = platform;
}
platform.bootstrapModule(AppModule)
.catch(err => console.error(err));将其添加到shell和mfe中的bootstrap.ts将使mfe重用现有的平台,而不是尝试创建一个新实例,这将导致您正在经历的错误。
https://stackoverflow.com/questions/67545533
复制相似问题