mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-18 03:00:58 +08:00
59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
import { LocationStrategy } from '@angular/common';
|
|
import { ElementRef, OnChanges, OnDestroy, OnInit, Directive, HostListener, Input, Optional } from '@angular/core';
|
|
import { Router, RouterLink } from '@angular/router';
|
|
import { AnimationBuilder, RouterDirection } from '@ionic/core';
|
|
import { Subscription } from 'rxjs';
|
|
|
|
import { NavController } from '../../providers/nav-controller';
|
|
|
|
@Directive({
|
|
selector: '[routerLink]',
|
|
})
|
|
export class RouterLinkDelegateDirective implements OnInit, OnChanges, OnDestroy {
|
|
private subscription?: Subscription;
|
|
|
|
@Input()
|
|
routerDirection: RouterDirection = 'forward';
|
|
|
|
@Input()
|
|
routerAnimation?: AnimationBuilder;
|
|
|
|
constructor(
|
|
private locationStrategy: LocationStrategy,
|
|
private navCtrl: NavController,
|
|
private elementRef: ElementRef,
|
|
private router: Router,
|
|
@Optional() private routerLink?: RouterLink
|
|
) {}
|
|
|
|
ngOnInit(): void {
|
|
this.updateTargetUrlAndHref();
|
|
}
|
|
|
|
ngOnChanges(): void {
|
|
this.updateTargetUrlAndHref();
|
|
}
|
|
|
|
ngOnDestroy(): void {
|
|
if (this.subscription) {
|
|
this.subscription.unsubscribe();
|
|
}
|
|
}
|
|
|
|
private updateTargetUrlAndHref() {
|
|
if (this.routerLink?.urlTree) {
|
|
const href = this.locationStrategy.prepareExternalUrl(this.router.serializeUrl(this.routerLink.urlTree));
|
|
this.elementRef.nativeElement.href = href;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @internal
|
|
*/
|
|
@HostListener('click', ['$event'])
|
|
onClick(ev: UIEvent): void {
|
|
this.navCtrl.setDirection(this.routerDirection, undefined, undefined, this.routerAnimation);
|
|
ev.preventDefault();
|
|
}
|
|
}
|