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

Niels Morf}

Niels Morf

Developer, Larper, Sailor, Human

Comments

Loading...

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:

  • Name (Visible to others)
  • E-Mail (Visible as md5 hash only when using the gravatar option)
  • Your IP-Adress
  • Date and time of your comment (Visible to others)
  • The content of your comment (Visible to others)
Your e-mail address may be used to contact you in response to your comment. Otherwise the data will only be used to display the comments.