-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(admin-ui): Fix support for canDeactivate guard on angular routes
- Loading branch information
1 parent
74a8c05
commit 6d9af1d
Showing
4 changed files
with
61 additions
and
14 deletions.
There are no files selected for viewing
52 changes: 49 additions & 3 deletions
52
packages/admin-ui/src/lib/core/src/extension/components/angular-route.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,59 @@ | ||
import { Component, inject } from '@angular/core'; | ||
import { | ||
Component, | ||
ComponentRef, | ||
EventEmitter, | ||
inject, | ||
Input, | ||
OnInit, | ||
Output, | ||
ViewContainerRef, | ||
} from '@angular/core'; | ||
import { SharedModule } from '../../shared/shared.module'; | ||
import { ROUTE_COMPONENT_OPTIONS, RouteComponent } from './route.component'; | ||
|
||
/** | ||
* @description | ||
* This component is used internally to allow us to dynamically load a component | ||
* like with `*ngComponentOutlet`, but with the ability to get a reference to the | ||
* created ComponentRef. This can then be used to delegate lifecycle events like | ||
* `canDeactivate` to the loaded component. | ||
*/ | ||
@Component({ | ||
selector: 'vdr-dynamic-component-loader', | ||
template: ``, | ||
standalone: true, | ||
imports: [SharedModule], | ||
}) | ||
export class DynamicComponentLoaderComponent implements OnInit { | ||
@Input() componentType: any; | ||
@Output() loaded = new EventEmitter<ComponentRef<any>>(); | ||
constructor(private viewContainer: ViewContainerRef) {} | ||
|
||
ngOnInit() { | ||
const componentRef = this.viewContainer.createComponent(this.componentType); | ||
this.loaded.emit(componentRef); | ||
} | ||
} | ||
|
||
@Component({ | ||
selector: 'vdr-angular-route-component', | ||
template: ` <vdr-route-component><ng-container *ngComponentOutlet="component" /></vdr-route-component> `, | ||
template: ` | ||
<vdr-route-component> | ||
<vdr-dynamic-component-loader [componentType]="component" (loaded)="componentLoaded($event)" /> | ||
</vdr-route-component> | ||
`, | ||
standalone: true, | ||
imports: [SharedModule, RouteComponent], | ||
imports: [SharedModule, RouteComponent, DynamicComponentLoaderComponent], | ||
}) | ||
export class AngularRouteComponent { | ||
protected component = inject(ROUTE_COMPONENT_OPTIONS).component; | ||
protected componentRef: ComponentRef<any>; | ||
|
||
componentLoaded(componentRef: ComponentRef<any>) { | ||
this.componentRef = componentRef; | ||
} | ||
|
||
canDeactivate() { | ||
return this.componentRef?.instance?.canDeactivate?.(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters