Aurelia 获取奥雷利亚的当前路线

Aurelia 获取奥雷利亚的当前路线,aurelia,Aurelia,要在非视图模型类中获取当前路由,最佳做法是注入路由器并使用此.Router.history.fragment吗?或者这是一个否定吗?您可以注入路由器并获取当前指令。像这样: import { inject } from 'aurelia-dependency-injection'; //or framework import { Router } from 'aurelia-router'; @inject(Router) export class MyClass { construc

要在非视图模型类中获取当前路由,最佳做法是注入路由器并使用此.Router.history.fragment吗?或者这是一个否定吗?

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

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
   }
}

请注意,
currentInstruction
在构造函数中仍然是
null
,因此如果要将其用于绑定,可以在
created
方法中获取它(另请参见此处的“我正在获取上一条路线的数据”,知道原因吗???谢谢。可能是因为视图尚未更改。请检查您是否具有
canDeactivate
功能我无法编辑自己的注释,但链接已更改。