获得奥雷利亚的当前路线 [英] Getting the current route in Aurelia
本文介绍了获得奥雷利亚的当前路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要在非视图模型类中获取当前路线,最佳实践是注入路由器并使用this.router.history.fragment吗?还是这不可以吗?
To get the current route within a non-view-model class, would the best practice be to inject the Router and use this.router.history.fragment? Or is this a no-no?
推荐答案
您可以注入路由器并获取当前指令.像这样:
You could inject the router and get the current instruction. Like this:
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
}
}
这篇关于获得奥雷利亚的当前路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文