mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-19 19:57:22 +08:00
fix(angular): routerLink updates href
This commit is contained in:

committed by
Manu MA

parent
49d8e16f2b
commit
c8d9685bc9
@ -1,12 +1,12 @@
|
||||
import { Directive, ElementRef, HostListener, Input, Optional } from '@angular/core';
|
||||
import { NavController, NavDirection } from '../../providers/nav-controller';
|
||||
import { NavigationEnd, Router, RouterLink } from '@angular/router';
|
||||
import { Router, RouterLink } from '@angular/router';
|
||||
import { LocationStrategy } from '@angular/common';
|
||||
import { Subscription } from 'rxjs';
|
||||
|
||||
|
||||
@Directive({
|
||||
selector: '[routerLink]'
|
||||
selector: '[routerLink]',
|
||||
})
|
||||
export class HrefDelegate {
|
||||
|
||||
@ -20,22 +20,10 @@ export class HrefDelegate {
|
||||
private navCtrl: NavController,
|
||||
private elementRef: ElementRef,
|
||||
@Optional() private routerLink?: RouterLink,
|
||||
) {
|
||||
if (this.routerLink) {
|
||||
this.subscription = router.events.subscribe(s => {
|
||||
if (s instanceof NavigationEnd) {
|
||||
this.updateTargetUrlAndHref();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
) { }
|
||||
|
||||
updateTargetUrlAndHref() {
|
||||
if (this.routerLink) {
|
||||
const href = this.locationStrategy.prepareExternalUrl(this.router.serializeUrl(this.routerLink.urlTree));
|
||||
console.log(href);
|
||||
this.elementRef.nativeElement.href = href;
|
||||
}
|
||||
ngOnInit() {
|
||||
this.updateTargetUrlAndHref();
|
||||
}
|
||||
|
||||
ngOnChanges(): any {
|
||||
@ -48,6 +36,14 @@ export class HrefDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
private updateTargetUrlAndHref() {
|
||||
if (this.routerLink) {
|
||||
const href = this.locationStrategy.prepareExternalUrl(this.router.serializeUrl(this.routerLink.urlTree));
|
||||
console.log(href);
|
||||
this.elementRef.nativeElement.href = href;
|
||||
}
|
||||
}
|
||||
|
||||
@HostListener('click', ['$event'])
|
||||
onClick(ev: UIEvent) {
|
||||
if (this.routerDirection) {
|
||||
|
@ -153,7 +153,7 @@ export class Item implements ComponentInterface {
|
||||
<TagType
|
||||
{...attrs}
|
||||
class="item-native"
|
||||
onClick={ev => openURL(win, href, ev, routerDirection)}
|
||||
onClick={(ev: Event) => openURL(win, href, ev, routerDirection)}
|
||||
>
|
||||
<slot name="start"></slot>
|
||||
<div class="item-inner">
|
||||
|
Reference in New Issue
Block a user