Aurelia: Getting the route template from child routers in a parent routers pipeline step
To track page views I needed a way to get the route templates from my child routes. The Problem: I wanted to get them in a pipline step from my parent router.
After some time trying I came up with solution to the problem. The code is quite simple:
import { NavigationInstruction } from "aurelia-router";
export function getRouteTemplateName(instruction: NavigationInstruction): string {
let instructions = instruction.getAllInstructions();
let route = "";
for (const instruction of instructions) {
let routeConfig = instruction.config.route;
if (typeof routeConfig === "string")
route += routeConfig.replace("*childRoute", "");
}
return route;
}
You can use the function like so:
import { PipelineStep, NavigationInstruction, Next } from "aurelia-router";
import { getRouteTemplateName } from "./routeNameFilter";
export class PreActivateStep implements PipelineStep {
run(instruction: NavigationInstruction, next: Next) {
console.log(getRouteTemplateName(instruction));
return next();
}
}
I hope you will find it usefull :)
Edits:
14 Juli 2018: spelling and some minor code readability improvements
Comments
New Comment
When clicking "Post" you agree to publish your comment publicly and have acknowledged the privacy implications below.
Privacy: The following information will be stored when you post a comment: