From 7187445873cf597ba682f75749ff5b4e4e6a6f59 Mon Sep 17 00:00:00 2001 From: markuczy Date: Tue, 16 Jul 2024 11:27:33 +0200 Subject: [PATCH] feat: unsubscribe for module --- .../lib/utils/webcomponent-bootstrap.utils.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/libs/angular-webcomponents/src/lib/utils/webcomponent-bootstrap.utils.ts b/libs/angular-webcomponents/src/lib/utils/webcomponent-bootstrap.utils.ts index ddffcf09..a3e4e086 100644 --- a/libs/angular-webcomponents/src/lib/utils/webcomponent-bootstrap.utils.ts +++ b/libs/angular-webcomponents/src/lib/utils/webcomponent-bootstrap.utils.ts @@ -1,7 +1,6 @@ import { createCustomElement } from '@angular/elements' import { createApplication, platformBrowser } from '@angular/platform-browser' import { - EnvironmentInjector, EnvironmentProviders, Injector, NgModuleRef, @@ -33,8 +32,6 @@ export function bootstrapModule(module: Type, appType: AppType, production .then((ref) => { if (appType === 'shell') { setShellZone(ref.injector) - } else if (appType === 'microfrontend') { - connectMicroFrontendRouter(ref.injector) } return ref }) @@ -65,16 +62,23 @@ export async function bootstrapRemoteComponent( createEntrypoint(component, elementName, app.injector, sub) } -export function createEntrypoint( +export function createAppEntrypoint(component: Type, elementName: string, injector: Injector) { + const sub = connectMicroFrontendRouter(injector) + createEntrypoint(component, elementName, injector, sub) +} + +function createEntrypoint( component: Type, elementName: string, - injector: EnvironmentInjector, + injector: Injector, routerSub?: Subscription | null ) { const originalNgDestroy = component.prototype.ngOnDestroy?.bind(component) component.prototype.ngOnDestroy = () => { routerSub?.unsubscribe() - originalNgDestroy() + if (originalNgDestroy !== undefined) { + originalNgDestroy() + } } const myRemoteComponentAsWebComponent = createCustomElement(component, {