-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcheckout.loaded-via-route-guard.component.ts
64 lines (58 loc) · 1.9 KB
/
checkout.loaded-via-route-guard.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
import {
CUSTOM_ELEMENTS_SCHEMA,
Component,
EventEmitter,
Input,
Output,
} from '@angular/core';
import { Routes } from '@angular/router';
import { remoteModuleGuard } from 'src/micro-frontends-tooling/remote-module.guard';
import { CheckoutService } from './checkout.service';
@Component({
selector: 'app-checkout-mfe',
standalone: true,
schemas: [CUSTOM_ELEMENTS_SCHEMA],
template: `
<mfe-checkout
[basketValue]="basketValue"
(checkout-requested)="checkoutHandler($event)">
</mfe-checkout>
`,
})
export class CheckoutComponent {
public constructor(private readonly _checkoutService: CheckoutService) {}
@Input()
public basketValue?: string;
@Output()
public checkoutRequested: EventEmitter<string> = new EventEmitter<string>();
public checkoutHandler(event: Event): void {
const checkoutEvent = event as CustomEvent<string>;
const checkoutMessage= checkoutEvent.detail;
// We don't need to both emit the checkout message and share it via
// the checkout service. Usually, you would choose one of them depending
// on how this mfe is used.
//
// Using the 'emit' function only allows the output to be consumed via
// parent HTML elements, whilst using a service to share the data doesn't
// have that limitation
//
// Alternatively, you could use an event bus as shown in the
// communication-event-bus-ng16 code demo.
this.checkoutRequested.emit(checkoutMessage);
this._checkoutService.triggerCheckoutRequested(checkoutMessage);
}
}
export const MFE_CHECKOUT_ROUTES: Routes = [
{
path: '**', // https://angular.io/guide/router#setting-up-wildcard-routes
component: CheckoutComponent,
canActivate: [
remoteModuleGuard({
id: CheckoutComponent.name,
type: "module",
remoteEntry: "http://localhost:4201/remoteEntry.js",
exposedModule: "./checkout-auto",
})
],
},
]