fix(angular): routerLink updates href

This commit is contained in:
Manu Mtz.-Almeida
2018-12-05 21:18:03 +01:00
committed by Manu MA
parent 49d8e16f2b
commit c8d9685bc9
2 changed files with 14 additions and 18 deletions

View File

@ -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) {

View File

@ -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">