如何获取当前的routeName? [英] How to get current routeName?
问题描述
假设我的应用程序绑定到 window.App
:
我可以得到它的路由器与 App .__ container __。lookup(router :主)
。
如何访问当前路由?具体来说,我想获得它的属性 routeName
。
它存在属性称为 currentPath
,它是应用程序当前状态的计算别名。要得到当前 routeName
这是同样的事情,你可以这样做:
App = Ember,Application.create({
currentPath:''
});
ApplicationController:Ember.Controller.extend({
currentPathDidChange:function(){
App.set('currentPath',this.get('currentPath'));
} .observes('currentPath')
});
然后从以下任何地方访问:
App.get( 'currentPath');
请参阅 here 了解更多有关 currentPath
property。
另外值得一提的是,您可以启用标志 LOG_TRANSITIONS
每次应用程序更改状态/路由时,当前路由名称都记录在浏览器控制台中:
App = Ember.Application.create {
LOG_TRANSITION:true
});
注意
使用此应用.__容器__。lookup(router:main)
仅用于调试目的,因为它是一个内部API。
希望它有帮助。 >
Assuming my application in bound to window.App
:
I can get it's router with App.__container__.lookup("router:main")
.
How can I access current route? Specifically I want to get its property routeName
.
It exist a property called currentPath
which is a computed alias for the current state of the application. To get the current routeName
which is the same thing you could do something like this:
App = Ember,Application.create({
currentPath: ''
});
ApplicationController : Ember.Controller.extend({
currentPathDidChange: function() {
App.set('currentPath', this.get('currentPath'));
}.observes('currentPath')
});
and then access it from anywhere with:
App.get('currentPath');
See here for more info for the currentPath
property.
Also worth mentioning is that you can enable the flag LOG_TRANSITIONS
to true to have the current route name logged in your browser console every time the application changes state/route:
App = Ember.Application.create({
LOG_TRANSITION: true
});
Note
Use this App.__container__.lookup("router:main")
only for debugging purposes since it's an internal API.
Hope it helps.
这篇关于如何获取当前的routeName?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!