在Aurelia获取当前路线

时间:2016-06-28 19:15:18

标签: aurelia aurelia-router

要在非视图模型类中获取当前路由,最佳做法是注入路由器并使用this.router.history.fragment?或者这是不是吗?

1 个答案:

答案 0 :(得分:25)

您可以注入路由器并获取当前指令。像这样:

import { inject } from 'aurelia-dependency-injection'; //or framework
import { Router } from 'aurelia-router';

@inject(Router)
export class MyClass {

   constructor(router) {
      this.router = router;
   }

   getRoute() {
     return this.router.currentInstruction.config.name; //name of the route
     //return this.router.currentInstruction.config.moduleId; //moduleId of the route
   }
}