Get current route name in Ember
NOTE: as of Ember 3.16, the original answer is not only recommended, but observers are strongly discouraged.
To get the current route name, you can utilize the Router Service: https://api.emberjs.com/ember/3.18/classes/RouterService/properties/currentRouteName?anchor=currentRouteName
export default class MyComponent extends Component {
@service router;
get activeRoute() {
return this.router.currentRouteName;
}
}
Original answer below
You could observe the application
's currentPath
and set it to the current route accordingly when it changes:
App = Ember.Application.create({
currentPath: '',
});
App.ApplicationController = Ember.Controller.extend({
updateCurrentPath: function() {
App.set('currentPath', this.get('currentPath'));
}.observes('currentPath')
}),
This way you have access to the currentPath
when ever you want with App.get('currentPath');
E.g.
if (App.get('currentPath') == 'home'){
this.transitionTo(userLang);
}
Hope it helps.
This worked for me on 1.3.0-beta (and a quick glance at the source for 1.1.2 suggests it would work there too):
App.__container__.lookup('router:main').location.lastSetURL
Note that the documentation states:
At present, it relies on a hashchange event existing in the browser.
However, I believe it's strongly suggested that App.__container__
not be used in production code. A more acceptable alternative would be to use App.Router.router.currentHandlerInfos
, which provides information on the current Ember route.
Yet another option is currentRouteName
on the ApplicationController
. You can add needs: ['application']
to your controller, then access the route name with controllers.application.currentRouteName
. This will return something like posts.index
.