Get current route in Aurelia 2 (router-lite)

Is there a way to get the current route id in Aurelia v2?

In Aurelia 1: this.router.currentInstruction.config.name

I found this, but there are no typings for this and it seems a bit hacky! :slight_smile:

canLoad(params: Params, next: RouteNode, current: RouteNode | null) {
	console.log(next.instruction.recognizedRoute.route.endpoint.route.handler['config'].id);
	console.log(next.fragment); // please also note, this is always null, is there a reason for this?

	return true;
}

This issue was originally from November 22. I adapted the issue for router-lite, @Sayan751, could you maybe help me?

3 Likes

Hi @elitastic! You are on the right path. Here is how you can get the type definition: router-lite - discourse 4969 - StackBlitz.

The RouteDefinition#config contains the route configuration object. Unfortunately, there is currently no more straightforward way to get the current route Id in router-lite.

Regarding the fragment being always null, can you please share a reproduction? At least in the SB demo I have shared here, I cannot reproduce that.

Oh, I think I missunderstood the fragment property. If I recall this correctly in the docs, the fragment property means the ‘#xyz’ portion (hashtag) of the url.

In Aurelia 1, the NavigationInstruction.fragment was the complete current url, as far as I know.

Note that the Router class also exposes a currentTr: Transition property, using which the current transition can be grabbed. Moreover, using the Transition#finalInstructions property, all the final/resolved viewport instructions can be viewed. Note that the instruction will only be finalized when the routing instruction is completely ran.

1 Like