2013-08-18 111 views
13

我需要在我的餘燼應用中獲取當前路由名稱;我試過這個: Ember App.Router.router.currentState undefined 但它不適用於我(有probablig東西我錯過了...)我使用Ember rc6,我有一個多語種的應用程序;在applicationRoute我檢測瀏覽器的語言和我重定向到正確的頁面:在Ember中獲取當前路由名稱

this.transitionTo(userLang);

,但我想這隻有當用戶在主頁上執行,所以是這樣的:

if (currentRoute == 'home'){ 
    this.transitionTo(userLang) 
} 
+0

[Emberjs - 如何訪問RC1中的當前狀態或路由名稱?](http://stackoverflow.com/questions/15318981/emberjs-how-to-access-the-current-state-or -route-name-in-rc1) – givanse

回答

17

你可以觀察到applicationcurrentPath,當它改變其相應的設置到當前的路線:

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

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

這樣,您可以隨時使用currentPathApp.get('currentPath');

例如,

if (App.get('currentPath') == 'home'){ 
    this.transitionTo(userLang); 
} 

希望它有幫助。

+0

那麼,事情是,如果我在控制器內執行'console.log(App.get(「currentPath」))',我可以看到它的工作原理;但我需要從ApplicationRoute的重定向方法內部訪問currentPath,並且在這裏它不起作用(這裏的控制檯日誌顯示currentPath沒有值)...可能只有在調用redirect方法後才更新currentPath .. –

+0

也做了一切從控制器工程(所以現在我不需要ApplicationRoute的重定向方法了);感謝您的幫助... –

+4

我想在Ember App Kit中執行此操作,其中'App'全局名稱空間已被替換爲ES6模塊轉譯器。由於App.set和App.get不起作用,我如何訪問'currentPath'?謝謝! – Chris

25

這爲我工作在1.3.0-β(和快速瀏覽一下源1.1.2表明,它會在那裏工作過):

App.__container__.lookup('router:main').location.lastSetURL 

注意的是,文件規定:

At present, it relies on a hashchange event existing in the browser. 

但是,我相信強烈建議App.__container__不能用於生產代碼。更可接受的替代方案是使用App.Router.router.currentHandlerInfos,它提供有關當前Ember路線的信息。

另一個選項是ApplicationController上的currentRouteName。您可以將needs: ['application']添加到您的控制器,然後使用controllers.application.currentRouteName訪問路由名稱。這將返回類似posts.index

+0

我可以確認它適用於1.2.0。但是你必須小心,在應用程序的初始加載之後它不起作用,即使它是深層鏈接;只有在過渡後,纔會返回正確的值。 – vanthome

+7

「App.Router.router.currentHandlerInfos」不僅包含有關當前Ember路線(包括其上下文)的信息(讀取:模型),還包含所有父路線的類似信息。如果你需要這些數據,這真的是要走的路! – nickiaconis

+0

'App .__ container __。lookup()'非常有用。 – datchung

3

目前作爲Ember 1.7.0,您可以通過致電this.routeName從路線中獲取當前路線。

13

就像Ember 1.8.1中的更新一樣,我們可以通過執行this.routeName來獲取Ember.Route對象內的routeName。

+0

如何在集成測試中獲得它? ** currentRouteName **未定義 – SuperUberDuper

+2

'this.routeName'在從應用程序路由中調用時返回「application」,不是很有用...類似route.sub.sub會很棒。 –

-2

您可以簡單地解析當前的URL。這樣你就可以使用完整的網址,例如:

http://127.0.0.1:8000/index.html/#/home 

,並從該字符串中提取後綴:

/home 

這是當前的路徑名。

一個簡單的JS函數(即作品無論您灰燼版本)將是:

function getCurrentRoute() 
{ 
    var currentRoute; 
    var currentUrl = window.location.href; // 'http://127.0.0.1:8000/index.html/#/home' 

    var indexOfHash = currentUrl.indexOf('#'); 
    if ((indexOfHash == -1) || 
     (indexOfHash == currentUrl.length - 1)) 
    { 
     currentRoute = '/'; 
    } 
    else 
    { 
     currentRoute = currentUrl.slice(indexOfHash + 1); // '/home' 
    } 

    return currentRoute; 
} 

使用示例:

if (getCurrentRoute() == '/home') 
{ 
// ... 
} 
17

隨着轉向組件,這是很難得的路線名稱。最好的辦法是在初始值設定/ router.js添加一個初始化諸如

ember g initializer router 

(來自命令行),和

export function initialize(application) { 
    application.inject('route', 'router', 'router:main'); 
    application.inject('component', 'router', 'router:main'); 
} 

export default { 
    name: 'router', 
    initialize 
}; 

。如果需要,您也可以注入控制器。然後,只需做簡單的

this.get('router.currentRouteName'); 
在JS

,或者

{{router.currentRouteName}} 

模板。

這是我發現得到它可靠的唯一途徑,並在灰燼2.4

+1

非常好,確實是先生 –

+0

這在Ember 2.7中爲我工作,非常感謝你!這對了解發生的事情也很有幫助 - 特別是關於觀察路線變化的最後一個答案:http://discuss.emberjs.com/t/ember-get-current-route-name/10324/7 – rmcsharry

+0

我用過這個信息幫助建立這個組件:http://stackoverflow.com/questions/39395911/emberjs-2-7-how-to-bind-attribute-disabled-for-a-button/39396958#39396958 – rmcsharry

0

觀察的我有同樣的問題了一會兒。然後我開始探索路由器。它始終有一個狀態對象,可以從任何路線使用

var route = this; 
var handlerInfos = route.get("router.router.state.handlerInfos"); 
var currRouteHandlerInfo = handlerInfos[handlerInfos.length-1]; 
var currRouteName = currRouteHandlerInfo.name; //"home" 

就是這樣。現在你有當前的路由名稱!

如果你想在當前路徑PARAMS

var routerParams = this.get("router.router.state.params"); 
var currRouteParams = routerParams[currRouteName]; //{ homeId : "1" } 
12

如果你想在你的組件或控制器,你可以注入路由服務(routing: Ember.inject.service('-routing')

(爲more),並利用現有的路線:

this.get('routing.currentRouteName')this.get('routing.currentPath')

與COM實施例Ponent(波納恩特)和計算性能:

import Ember from 'ember'; 

export default Ember.Component.extend({ 
    routing: Ember.inject.service('-routing'), 

    checkMyRouteName: Ember.computed(function() { 
    return this.get('routing.currentRouteName'); 
    }) 
}) 

例與控制器和計算性能:

import Ember from 'ember'; 

export default Ember.Controller.extend({ 
    routing: Ember.inject.service('-routing'), 

    checkMyRouteName: Ember.computed(function() { 
    return this.get('routing.currentRouteName'); 
    }) 
}) 

當前路線在你的路線,你只需要this.routeName

例與路線

import Ember from 'ember'; 

export default Ember.Route.extend({ 
    checkMyRouteName() { 
    return this.routeName; 
    } 
}) 
3

Ember命名空間API現在有一個getOwner方法,這對於查找currentRouteName或其他路由屬性非常有用。

const owner  = Ember.getOwner(this); 
    const currentRoute = owner.lookup('router:main').currentRouteName; 
    const routeInfo = owner.lookup(`route:${currentRoute}`).get('info'); 
    // etc. 

我創建了一個Ember Twiddle示例來演示。使用「輸出」窗格上方的文本輸入點擊其他路線,如/blue/green/red

相關問題