Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get reference to the current router from ViewModel

Tags:

aurelia

In my viewmodel class, how do I get a reference to the current Router?

What I really want to do is get the current ModuleId.

In Durandal, there was system.getModuleId, but there is no system in Durandal, so I figure the router is going to have that information.

like image 654
Greg Gum Avatar asked Jul 16 '15 19:07

Greg Gum


2 Answers

The way to get a reference to the current router:

import {inject} from 'aurelia-framework'
import {router} from 'aurelia-router'

@inject(router)
constructor(router)
{
    this.router = router; //Do something with router.
}

Note: Do not inject "AppRouter" It's a different router. If you add a route to AppRouter, it will not work. It will work if you import Router.

like image 150
Greg Gum Avatar answered Sep 21 '22 12:09

Greg Gum


One way (not sure, the optimal one) to access current moduleId is in activate hook of your class:

activate(params, routeConfig) {
    console.log(routeConfig.moduleId);
}
like image 23
dfsq Avatar answered Sep 19 '22 12:09

dfsq