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

committed by
Manu MA

parent
959ce65323
commit
63cbc92741
@ -1,42 +1,59 @@
|
|||||||
import { Directive, ElementRef, HostListener, Input, Optional } from '@angular/core';
|
import { Directive, ElementRef, HostListener, Input, Optional } from '@angular/core';
|
||||||
import { Router } from '@angular/router';
|
|
||||||
import { NavController, NavDirection } from '../../providers/nav-controller';
|
import { NavController, NavDirection } from '../../providers/nav-controller';
|
||||||
|
import { NavigationEnd, Router, RouterLink } from '@angular/router';
|
||||||
|
import { LocationStrategy } from '@angular/common';
|
||||||
|
import { Subscription } from 'rxjs';
|
||||||
|
|
||||||
|
|
||||||
@Directive({
|
@Directive({
|
||||||
selector: '[routerLink],[routerDirection],ion-anchor,ion-button,ion-item'
|
selector: '[routerLink]'
|
||||||
})
|
})
|
||||||
export class HrefDelegate {
|
export class HrefDelegate {
|
||||||
|
|
||||||
@Input() routerLink: any;
|
private subscription?: Subscription;
|
||||||
|
|
||||||
@Input() routerDirection: NavDirection = 'forward';
|
@Input() routerDirection: NavDirection = 'forward';
|
||||||
|
|
||||||
@Input()
|
constructor(
|
||||||
set href(value: string) {
|
private router: Router,
|
||||||
this.elementRef.nativeElement.href = value;
|
private locationStrategy: LocationStrategy,
|
||||||
}
|
private navCtrl: NavController,
|
||||||
get href() {
|
private elementRef: ElementRef,
|
||||||
return this.elementRef.nativeElement.href;
|
@Optional() private routerLink?: RouterLink,
|
||||||
|
) {
|
||||||
|
if (this.routerLink) {
|
||||||
|
this.subscription = router.events.subscribe(s => {
|
||||||
|
if (s instanceof NavigationEnd) {
|
||||||
|
this.updateTargetUrlAndHref();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(
|
updateTargetUrlAndHref() {
|
||||||
@Optional() private router: Router,
|
if (this.routerLink) {
|
||||||
private navCtrl: NavController,
|
const href = this.locationStrategy.prepareExternalUrl(this.router.serializeUrl(this.routerLink.urlTree));
|
||||||
private elementRef: ElementRef
|
console.log(href);
|
||||||
) {}
|
this.elementRef.nativeElement.href = href;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ngOnChanges(): any {
|
||||||
|
this.updateTargetUrlAndHref();
|
||||||
|
}
|
||||||
|
|
||||||
|
ngOnDestroy(): any {
|
||||||
|
if (this.subscription) {
|
||||||
|
this.subscription.unsubscribe();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@HostListener('click', ['$event'])
|
@HostListener('click', ['$event'])
|
||||||
onClick(ev: Event) {
|
onClick(ev: UIEvent) {
|
||||||
const url = this.href;
|
|
||||||
if (this.routerDirection) {
|
if (this.routerDirection) {
|
||||||
this.navCtrl.setDirection(this.routerDirection);
|
this.navCtrl.setDirection(this.routerDirection);
|
||||||
}
|
}
|
||||||
|
ev.preventDefault();
|
||||||
if (!this.routerLink && this.router && url != null && url[0] !== '#' && !SCHEME.test(url)) {
|
|
||||||
ev.preventDefault();
|
|
||||||
this.router.navigateByUrl(url);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const SCHEME = /^[a-z][a-z0-9+\-.]*:/;
|
|
||||||
|
@ -17,10 +17,6 @@ describe('router-link', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('forward', () => {
|
describe('forward', () => {
|
||||||
it('should go forward with ion-button[href]', async () => {
|
|
||||||
await element(by.css('#href')).click();
|
|
||||||
await testForward();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should go forward with ion-button[routerLink]', async () => {
|
it('should go forward with ion-button[routerLink]', async () => {
|
||||||
await element(by.css('#routerLink')).click();
|
await element(by.css('#routerLink')).click();
|
||||||
@ -44,10 +40,6 @@ describe('router-link', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('root', () => {
|
describe('root', () => {
|
||||||
it('should go root with ion-button[href][routerDirection=root]', async () => {
|
|
||||||
await element(by.css('#href-root')).click();
|
|
||||||
await testRoot();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should go root with ion-button[routerLink][routerDirection=root]', async () => {
|
it('should go root with ion-button[routerLink][routerDirection=root]', async () => {
|
||||||
await element(by.css('#routerLink-root')).click();
|
await element(by.css('#routerLink-root')).click();
|
||||||
@ -66,10 +58,6 @@ describe('router-link', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('back', () => {
|
describe('back', () => {
|
||||||
it('should go back with ion-button[href][routerDirection=back]', async () => {
|
|
||||||
await element(by.css('#href-back')).click();
|
|
||||||
await testBack();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should go back with ion-button[routerLink][routerDirection=back]', async () => {
|
it('should go back with ion-button[routerLink][routerDirection=back]', async () => {
|
||||||
await element(by.css('#routerLink-back')).click();
|
await element(by.css('#routerLink-back')).click();
|
||||||
|
@ -11,11 +11,6 @@
|
|||||||
<p>ionViewWillLeave: <span id="ionViewWillLeave">{{willLeave}}</span></p>
|
<p>ionViewWillLeave: <span id="ionViewWillLeave">{{willLeave}}</span></p>
|
||||||
<p>ionViewDidLeave: <span id="ionViewDidLeave">{{didLeave}}</span></p>
|
<p>ionViewDidLeave: <span id="ionViewDidLeave">{{didLeave}}</span></p>
|
||||||
|
|
||||||
<p>
|
|
||||||
<ion-button href="/router-link-page" expand="block" id="href">ion-button[href]</ion-button>
|
|
||||||
<ion-button href="/router-link-page" routerDirection="root" expand="block" id="href-root">ion-button[href] (direction:root)</ion-button>
|
|
||||||
<ion-button href="/router-link-page" routerDirection="back" expand="block" id="href-back">ion-button[href] (direction:back)</ion-button>
|
|
||||||
</p>
|
|
||||||
<p>
|
<p>
|
||||||
<ion-button routerLink="/router-link-page" expand="block" color="dark" id="routerLink">ion-button[routerLink]</ion-button>
|
<ion-button routerLink="/router-link-page" expand="block" color="dark" id="routerLink">ion-button[routerLink]</ion-button>
|
||||||
<ion-button routerLink="/router-link-page" routerDirection="root" expand="block" color="dark" id="routerLink-root">ion-button[routerLink] (direction:root)</ion-button>
|
<ion-button routerLink="/router-link-page" routerDirection="root" expand="block" color="dark" id="routerLink-root">ion-button[routerLink] (direction:root)</ion-button>
|
||||||
|
Reference in New Issue
Block a user