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