mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-18 03:00:58 +08:00
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:
@ -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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user