aurelia icon indicating copy to clipboard operation
aurelia copied to clipboard

The navigation.previous property does not have existing route

Open Vheissu opened this issue 2 years ago • 0 comments

🐛 Bug Report

When accessing the navigation.previous property from within a component router lifecycle hook, you cannot get the previous route.

import { IRouteableComponent, Parameters, RoutingInstruction, Navigation } from '@aurelia/router';

export class AboutPage implements IRouteableComponent {
    load(parameters: Parameters, instruction: RoutingInstruction, navigation: Navigation) {
        console.log('Instructtion', instruction);
        console.log(navigation.previous);
    }
}

The only value that indicates the previous route is the match property. However, I would expect to be able to access the previous route object (it's id, any data, its path, etc).

It is worth noting the instruction parameter gives you the current route, but navigation.previous does not.

Vheissu avatar Apr 30 '22 11:04 Vheissu

@jwx Are there any plans to support getting the existing route on the previous property?

Vheissu avatar Sep 06 '22 06:09 Vheissu

@Vheissu The instruction parameter specifies the RoutingInstruction that resulted in the load for the viewport. It contains a route property that holds configured route data (if the load is the result of a configured route) including the matched configured route in the match property. The instruction also contains a previous property that holds the RoutingInstruction that resulted in the previous load.

The navigation parameter specifies the Navigation that resulted in the one or more RoutingInstruction(s) for the viewport(s). It contains a path property that specifies the path, if any, and an instruction property that were the reason for the navigation. The navigation also contains a previous property that holds the Navigation that resulted in the previous navigation.

Do you have a use case that's not covered by this?

jwx avatar Sep 07 '22 21:09 jwx

I guess I was just looking for a less convoluted solution. A property that gives you the previously loaded instruction without having to go and find it yourself. I'm just spoiled when it comes to how easy Aurelia makes a lot of tasks without needing to go diving too deeply @jwx

Vheissu avatar Sep 07 '22 23:09 Vheissu

If you want the instruction responsible for the previously loaded component in a viewport, you can get it with

load(_params, instruction) {
  console.log(instruction.previous);
}

That doesn't feel convoluted to me. I suspect I'm misunderstanding the use case. How would you have liked to find it?

jwx avatar Sep 07 '22 23:09 jwx

The previous route can be found instruction.previous.route.match it's a bit wordy, but it works. I think initially, I might have been looking for a simple one-liner to access the previous route to get its configuration details (ID, etc).

Vheissu avatar Sep 08 '22 00:09 Vheissu