如何获取当前routeName?

chr*_*mod 2 ember.js

假设我的应用程序必然window.App:我可以得到它的路由器App.__container__.lookup("router:main").

我如何访问当前路线?具体来说,我想得到它的财产routeName.

int*_*xel 10

它存在一个名为property的属性currentPath,它是应用程序当前状态的计算别名.要获得routeName同样的东西你可以做这样的事情:

App = Ember,Application.create({
  currentPath: ''
});

ApplicationController : Ember.Controller.extend({
  currentPathDidChange: function() {
    App.set('currentPath', this.get('currentPath'));
  }.observes('currentPath')
});
Run Code Online (Sandbox Code Playgroud)

然后从任何地方访问它:

App.get('currentPath');
Run Code Online (Sandbox Code Playgroud)

有关该酒店的更多信息,请参阅此处currentPath.

另外值得一提的是,LOG_TRANSITIONS每次应用程序更改状态/路由时,都可以将标志设置为true,以便在浏览器控制台中记录当前路由名称:

App = Ember.Application.create({
  LOG_TRANSITION: true
});
Run Code Online (Sandbox Code Playgroud)

注意App.__container__.lookup("router:main")仅 将此用于调试目的,因为它是内部API.

希望能帮助到你.