2013-06-21 197 views
2

假設我的應用綁定到window.App: 我可以得到它的路由器App.__container__.lookup("router:main")如何獲取當前路由名稱?

如何訪問當前路線?具體而言,我想獲得其屬性routeName

回答

10

它存在一個名爲currentPath的屬性,它是應用程序當前狀態的一個計算別名。爲了得到當前routeName這是你可以做這樣的事情同樣的事情:

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

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

,然後用從任何地方訪問它:

App.get('currentPath'); 

用於爲currentPath房產的更多信息,請參見here

另外值得一提的是,您可以啓用標誌LOG_TRANSITIONS爲true,將當前路徑名稱在您的瀏覽器控制檯的每個應用程序更改狀態/路由時登錄:

App = Ember.Application.create({ 
    LOG_TRANSITION: true 
}); 

注意 使用此App.__container__.lookup("router:main")僅用於調試目的,因爲它是一個內部API。

希望它有幫助。

+0

我正在使用'App .__ container __。lookup'來修復Ember核心中的錯誤,因此我無法擴展應用程序控制器。我可以在哪裏找到「currentState」?它不是「App」或「Router」的屬性。 – chrmod

+0

鏈接如果非常有幫助,謝謝。但如何從'App'訪問'StateManager'? – chrmod

+0

'StateManager'用於路由器的以前版本,現在在* v2 *中,路由器自己的工作是路由等。ApplicationController上的'currentPath'屬性在這裏處理:https:// github。 com/emberjs/ember.js/blob/v1.0.0-rc.5/packages/ember-routing/lib/system/router.js#L75 – intuitivepixel

相關問題