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 child my 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 ins of instructions) {
        let routeConfig = ins.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 this usefull :)

Niels Morf}

Niels Morf

Developer, Larper, Sailor, Human