简体   繁体   English

在Ember.js 2中的车把模板上获取当前路线名称

[英]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 . 我需要知道当前的路由名称,以便显示或隐藏在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. 获取当前路线名称,如果其名称等于“ posts”,则不会显示header-bar组件。

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 . applicatiion控制器具有属性名称currentRouteName You can use it. 您可以使用它。

If you want to use in another template you need to define that in controller. 如果要在另一个模板中使用,则需要在控制器中定义它。

test.js (controller) test.js(控制器)

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

test.hbs test.hbs

{{appCtrl.currentRouteName}}

Same answer as Ebrahim Pasbani. 答案与Ebrahim Pasbani相同。

This syntax is still usable in latest Ember version: 此语法在最新的Ember版本中仍然可用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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