-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpayment.loaded-via-directive.component.ts
78 lines (70 loc) · 2.72 KB
/
payment.loaded-via-directive.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import {
Component,
ComponentRef,
Type,
ViewChild,
ViewContainerRef,
} from '@angular/core';
import {
RemoteModuleDirective,
RemoteModuleDirectiveOptions,
} from 'src/micro-frontends-tooling/remote-module.directive';
import { Routes } from '@angular/router';
@Component({
selector: 'app-payment-mfe',
standalone: true,
imports: [RemoteModuleDirective],
template: `
<ng-container
#mfePayment
remoteModule
[remoteModuleoptions]="remoteModuleOptions",
[loadRemoteModuleCallback]="loadRemoteModuleHandler.bind(this)"
></ng-container>
<!--
The '.bind' call used in 'loadRemoteModuleHandler.bind(this)' above is required
to flow the 'this' context when the 'loadRemoteModuleHandler' method is executed.
Otherwise when the 'this._mfePaymentViewContainerRef' line of the
'loadRemoteModuleHandler' method is executed, the 'this' variable would be undefined.
Alternatively to using the '.bind' method, you could implement the 'loadRemoteModuleHandler'
as an arrow function. See commented out code below.
For more info see "Understanding This, Bind, Call, and Apply in JavaScript":
https://www.digitalocean.com/community/conceptual-articles/understanding-this-bind-call-and-apply-in-javascript
-->
`,
})
export class PaymentComponent {
@ViewChild('mfePayment', { read: ViewContainerRef, static: true })
private readonly _mfePaymentViewContainerRef?: ViewContainerRef;
public readonly remoteModuleOptions: RemoteModuleDirectiveOptions = {
id: PaymentComponent.name,
type: "module",
exposedModule: "./payment",
remoteEntry: "http://localhost:4202/remoteEntry.js",
};
// On the HTML template above, instead of using the `bind` method on the
// `loadRemoteModuleHandler.bind(this)` line, you could have just `loadRemoteModuleHandler`
// if you declare it as an arrow function like this:
//
// public loadRemoteModuleHandler = async (webpackModule: any): Promise<void> => {
// // in here the `this` variable is not undefined and points
// // to the expected PaymentComponent instance so everything
// // works as intended.
// }
public loadRemoteModuleHandler(webpackModule: any): void {
if (!this._mfePaymentViewContainerRef) {
return;
}
this._mfePaymentViewContainerRef.clear();
const paymentComponentType: Type<any> = webpackModule.PaymentComponent;
const mfePaymentComponentRef: ComponentRef<any> = this._mfePaymentViewContainerRef.createComponent(paymentComponentType);
// to set inputs use componentRef.setInput method
// to subscribe to outputs use componentRef.instance.<output>.subscribe(...)
}
}
export const MFE_PAYMENT_ROUTES: Routes = [
{
path: '**',
component: PaymentComponent,
},
]