Skip to content

Commit 91868e9

Browse files
committed
Propagate route from shell to microfrontend
1 parent 26ed84e commit 91868e9

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

projects/bookings/src/app/booking.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { BookingComponent } from './booking.component';
1616
FormsModule,
1717
RouterModule.forRoot([
1818
{
19-
path: 'journey/:journeyId',
19+
path: 'bookings/journey/:journeyId',
2020
component: BookingFormComponent,
2121
data: { title: 'Book journey' },
2222
},
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
import { Component } from '@angular/core';
1+
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
2+
import { Router } from '@angular/router';
23

34
@Component({
45
template: '<mf-booking></mf-booking>',
56
})
6-
export class EntryComponent {}
7+
export class EntryComponent implements OnChanges {
8+
@Input() route?: string;
9+
10+
constructor(private router: Router) {}
11+
12+
ngOnChanges(changes: SimpleChanges) {
13+
if (changes.route && this.route) {
14+
this.router.navigateByUrl(this.route, { state: { fromPlatform: true } });
15+
}
16+
}
17+
}

projects/train-platform/src/micro-frontends/bookings-host.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import { Component } from '@angular/core';
22

33
@Component({
44
selector: 'app-bookings-host',
5-
template: ` <mf-bookings-entry></mf-bookings-entry> `,
5+
template: ` <mf-bookings-entry microFrontendRouting></mf-bookings-entry> `,
66
})
77
export class BookingsHostComponent {}

projects/train-platform/src/micro-frontends/bookings-host.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { CUSTOM_ELEMENTS_SCHEMA, NgModule } from '@angular/core';
22
import { RouterModule } from '@angular/router';
33
import { BookingsHostComponent } from './bookings-host.component';
44
import { LoadMicroFrontendGuard } from './load-micro-frontend.guard';
5+
import { MicroFrontendRoutingDirective } from './micro-frontend-routing.directive';
56

67
@NgModule({
7-
declarations: [BookingsHostComponent],
8+
declarations: [BookingsHostComponent, MicroFrontendRoutingDirective],
89
imports: [
910
RouterModule.forChild([
1011
{
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Directive, ElementRef, OnDestroy, OnInit } from '@angular/core';
2+
import { ActivatedRoute, Router } from '@angular/router';
3+
import { Subject } from 'rxjs';
4+
import { map, takeUntil } from 'rxjs/operators';
5+
6+
@Directive({
7+
// eslint-disable-next-line @angular-eslint/directive-selector
8+
selector: '[microFrontendRouting]',
9+
})
10+
export class MicroFrontendRoutingDirective implements OnInit, OnDestroy {
11+
private destroyed$ = new Subject<void>();
12+
13+
constructor(
14+
private element: ElementRef,
15+
private router: Router,
16+
private route: ActivatedRoute
17+
) {}
18+
19+
ngOnInit(): void {
20+
this.route.url
21+
.pipe(
22+
map(() => this.router.url),
23+
takeUntil(this.destroyed$)
24+
)
25+
.subscribe((url) => (this.element.nativeElement.route = url));
26+
}
27+
28+
ngOnDestroy(): void {
29+
this.destroyed$.next();
30+
}
31+
}

0 commit comments

Comments
 (0)