要在非视图模型类中获取当前路由,最佳做法是注入路由器并使用this.router.history.fragment?或者这是不是吗?
答案 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
}
}