fix(angular): routerLink allows opening in a new tab for link elements (#25014)

Resolves #24413

Co-authored-by: Julian Pfeil <5290648+Juarrow@users.noreply.github.com>
This commit is contained in:
Sean Perkins
2022-03-31 09:14:33 -04:00
committed by GitHub
parent e8fc4ec397
commit b010f077fe

View File

@ -5,6 +5,12 @@ import { AnimationBuilder, RouterDirection } from '@ionic/core';
import { NavController } from '../../providers/nav-controller'; import { NavController } from '../../providers/nav-controller';
/**
* Adds support for Ionic routing directions and animations to the base Angular router link directive.
*
* When the router link is clicked, the directive will assign the direction and
* animation so that the routing integration will transition correctly.
*/
@Directive({ @Directive({
selector: '[routerLink]', selector: '[routerLink]',
}) })
@ -31,19 +37,15 @@ export class RouterLinkDelegateDirective implements OnInit, OnChanges {
this.updateTargetUrlAndHref(); this.updateTargetUrlAndHref();
} }
@HostListener('click')
onClick(): void {
this.navCtrl.setDirection(this.routerDirection, undefined, undefined, this.routerAnimation);
}
private updateTargetUrlAndHref() { private updateTargetUrlAndHref() {
if (this.routerLink?.urlTree) { if (this.routerLink?.urlTree) {
const href = this.locationStrategy.prepareExternalUrl(this.router.serializeUrl(this.routerLink.urlTree)); const href = this.locationStrategy.prepareExternalUrl(this.router.serializeUrl(this.routerLink.urlTree));
this.elementRef.nativeElement.href = href; this.elementRef.nativeElement.href = href;
} }
} }
/**
* @internal
*/
@HostListener('click', ['$event'])
onClick(ev: UIEvent): void {
this.navCtrl.setDirection(this.routerDirection, undefined, undefined, this.routerAnimation);
ev.preventDefault();
}
} }