fix(angular): button components now route correctly without reload (#25071)

This commit is contained in:
Liam DeBeasi
2022-04-07 11:57:15 -04:00
committed by GitHub
parent 72dd51ad78
commit 1c26e9b9b0
3 changed files with 68 additions and 5 deletions

View File

@ -12,7 +12,7 @@ import { NavController } from '../../providers/nav-controller';
* animation so that the routing integration will transition correctly. * animation so that the routing integration will transition correctly.
*/ */
@Directive({ @Directive({
selector: '[routerLink]', selector: ':not(a):not(area)[routerLink]',
}) })
export class RouterLinkDelegateDirective implements OnInit, OnChanges { export class RouterLinkDelegateDirective implements OnInit, OnChanges {
@Input() @Input()
@ -37,9 +37,56 @@ export class RouterLinkDelegateDirective implements OnInit, OnChanges {
this.updateTargetUrlAndHref(); this.updateTargetUrlAndHref();
} }
@HostListener('click') private updateTargetUrlAndHref() {
onClick(): void { if (this.routerLink?.urlTree) {
const href = this.locationStrategy.prepareExternalUrl(this.router.serializeUrl(this.routerLink.urlTree));
this.elementRef.nativeElement.href = href;
}
}
/**
* @internal
*/
@HostListener('click', ['$event'])
onClick(ev: UIEvent): void {
this.navCtrl.setDirection(this.routerDirection, undefined, undefined, this.routerAnimation); this.navCtrl.setDirection(this.routerDirection, undefined, undefined, this.routerAnimation);
/**
* This prevents the browser from
* performing a page reload when pressing
* an Ionic component with routerLink.
* The page reload interferes with routing
* and causes ion-back-button to disappear
* since the local history is wiped on reload.
*/
ev.preventDefault();
}
}
@Directive({
selector: 'a[routerLink],area[routerLink]',
})
export class RouterLinkWithHrefDelegateDirective implements OnInit, OnChanges {
@Input()
routerDirection: RouterDirection = 'forward';
@Input()
routerAnimation?: AnimationBuilder;
constructor(
private locationStrategy: LocationStrategy,
private navCtrl: NavController,
private elementRef: ElementRef,
private router: Router,
@Optional() private routerLink?: RouterLink
) {}
ngOnInit(): void {
this.updateTargetUrlAndHref();
}
ngOnChanges(): void {
this.updateTargetUrlAndHref();
} }
private updateTargetUrlAndHref() { private updateTargetUrlAndHref() {
@ -48,4 +95,12 @@ export class RouterLinkDelegateDirective implements OnInit, OnChanges {
this.elementRef.nativeElement.href = href; this.elementRef.nativeElement.href = href;
} }
} }
/**
* @internal
*/
@HostListener('click')
onClick(): void {
this.navCtrl.setDirection(this.routerDirection, undefined, undefined, this.routerAnimation);
}
} }

View File

@ -8,7 +8,11 @@ export { IonTabs } from './directives/navigation/ion-tabs';
export { IonBackButtonDelegateDirective as IonBackButtonDelegate } from './directives/navigation/ion-back-button'; export { IonBackButtonDelegateDirective as IonBackButtonDelegate } from './directives/navigation/ion-back-button';
export { NavDelegate } from './directives/navigation/nav-delegate'; export { NavDelegate } from './directives/navigation/nav-delegate';
export { IonRouterOutlet } from './directives/navigation/ion-router-outlet'; export { IonRouterOutlet } from './directives/navigation/ion-router-outlet';
export { RouterLinkDelegateDirective as RouterLinkDelegate } from './directives/navigation/router-link-delegate'; export {
RouterLinkDelegateDirective as RouterLinkDelegate,
RouterLinkWithHrefDelegateDirective as RouterLinkWithHrefDelegate,
} from './directives/navigation/router-link-delegate';
export { NavParams } from './directives/navigation/nav-params'; export { NavParams } from './directives/navigation/nav-params';
export { IonVirtualScroll } from './directives/virtual-scroll/virtual-scroll'; export { IonVirtualScroll } from './directives/virtual-scroll/virtual-scroll';
export { VirtualItem } from './directives/virtual-scroll/virtual-item'; export { VirtualItem } from './directives/virtual-scroll/virtual-item';

View File

@ -12,7 +12,10 @@ import { IonBackButtonDelegateDirective } from './directives/navigation/ion-back
import { IonRouterOutlet } from './directives/navigation/ion-router-outlet'; import { IonRouterOutlet } from './directives/navigation/ion-router-outlet';
import { IonTabs } from './directives/navigation/ion-tabs'; import { IonTabs } from './directives/navigation/ion-tabs';
import { NavDelegate } from './directives/navigation/nav-delegate'; import { NavDelegate } from './directives/navigation/nav-delegate';
import { RouterLinkDelegateDirective } from './directives/navigation/router-link-delegate'; import {
RouterLinkDelegateDirective,
RouterLinkWithHrefDelegateDirective,
} from './directives/navigation/router-link-delegate';
import { IonModal } from './directives/overlays/modal'; import { IonModal } from './directives/overlays/modal';
import { IonPopover } from './directives/overlays/popover'; import { IonPopover } from './directives/overlays/popover';
import { import {
@ -195,6 +198,7 @@ const DECLARATIONS = [
IonBackButtonDelegateDirective, IonBackButtonDelegateDirective,
NavDelegate, NavDelegate,
RouterLinkDelegateDirective, RouterLinkDelegateDirective,
RouterLinkWithHrefDelegateDirective,
// virtual scroll // virtual scroll
VirtualFooter, VirtualFooter,