bug: NavController direction gets lost when an Angular route guard returns a UrlTree

Offen
#31,071 2 Kommentare 4 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
52/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Ruhig
Tech-Stack
angular, typescript
Bereich
frontend

Rechercherichtung

Beginne damit, NavController.navigateRoot durch den Angular-Route-Guard-Ablauf zu verfolgen, mit Fokus auf NavigationCancel und die anschließende UrlTree-Navigation. Verwende die im Issue verlinkte Reproduktion und prüfe IonRouterOutlet.canGoBack. Als erledigt gilt die Änderung, wenn eine Guard-Umleitung die beabsichtigte Navigationsrichtung beibehält und den vorhandenen Stack nach der erfolgreichen Navigation entfernt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

package: angular type: bug
Prerequisites
Ionic Framework Version

v8.x

Current Behavior

When using NavController.navigateRoot and an Angular Route Guard returns a UrlTree, the existing pages in the navigation stack remain.

Expected Behavior

When using NavController.navigateRoot and an Angular Route Guard returns a UrlTree, the existing pages in the navigation stack are removed.

Steps to Reproduce
  1. Install @ionic/angular and @ionic/core >= 8.7.18
  2. Setup an Angular route with a Route Guard that returns a UrlTree
  3. Use NavController.navigateRoot to navigate to that route
  4. Check the navigation stack, for example with IonRouterOutlet.canGoBack
Code Reproduction URL

https://github.com/ionic-team/ionic-framework/pull/30955/changes/ca03a6d5f7a0d57ccb6db29284338152468894f2

Ionic Info

Ionic:

Ionic CLI : 6.20.1
Ionic Framework : not installed
@angular-devkit/build-angular : 21.2.2
@angular-devkit/schematics : 21.2.2
@angular/cli : 21.2.2
@ionic/angular-toolkit : not installed

Capacitor:

Capacitor CLI : 8.1.0
@capacitor/android : 8.1.0
@capacitor/core : 8.1.0
@capacitor/ios : 8.1.0

Utility:

cordova-res : not installed globally
native-run : not installed globally

System:

NodeJS : v22.22.0
npm : 10.9.4
OS : macOS

Additional Information

The issue was introduced in Ionic 8.7.18 with https://github.com/ionic-team/ionic-framework/pull/30955. Because of that PR, a NavigationCancel leads to the NavController direction being lost. This is not necessarily bad, but it interacts poorly with Angular Route Guard redirects. When such a Route Guard returns a UrlTree, the initial navigation gets cancelled and a new navigation to that UrlTree gets started. Unfortunately, that new navigation loses the information about the NavController direction because of the intermittent NavigationCancel.

Preferred solution:
Detect whether the NavigationCancel was caused by a Route Guard redirecting somewhere else and in those cases retain the NavController direction.

Alternative solution:
If our preferred solution is not feasible, please provide guidance on how to achieve the intended outcome. In our case: Start a navigation such that if the Route Guards lead to a successful navigation, the rest of the navigation stack is removed.

Vorherrschende Sprache
TypeScript
Sterne
52.8k
Forks
13.3k
Ø Merge
3 T. 1 Std.
Gemergte PRs (30 T.)
86

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus ionic-team/ionic-framework

Alle Issues in ionic-team/ionic-framework

Ähnliche Issues

Weitere Issues zu TypeScript

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.