简体   繁体   中英

Get current route name on handlebar template in Ember.js 2

I needed to know the current route name so I can show or hide a global component that is created on my application.hbs .

This component should be hidden in just one route template, so this looks like the easier solution to me. Getting the current route name and if its equal "posts" it will not show the header-bar component.

Something like this:

{{#if currentRoute != 'login'}}
    {{header-bar}}
{{/if}}

{{outlet}}

Any idea on could I achieve this or something similar that solves my problem?

applicatiion controller has a property names currentRouteName . You can use it.

If you want to use in another template you need to define that in controller.

test.js (controller)

init(){
  this._super(...arguments);
  let appCtrl = Ember.getOwner(this).lookup('controller:application');
  this.set('appCtrl', appCtrl);
}

test.hbs

{{appCtrl.currentRouteName}}

Same answer as Ebrahim Pasbani.

This syntax is still usable in latest Ember version:

App.__container__.lookup('controller:application').currentRouteName;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM