2016-07-06 30 views
2

我需要知道当前的路由名,以便我可以显示或隐藏在我的application.hbs上创建的全局组件。在Ember.js中获取车把模板中的当前路径名2

这个组件应该隐藏在一个路由模板中,所以这看起来对我来说更容易。获取当前的路由名称,如果其等于“帖子”,则不会显示header-bar组件。

事情是这样的:

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

{{outlet}} 

上,我可以做到这一点或类似的东西,解决我的问题你知道吗?

回答

3

applicatiion控制器有一个属性名称currentRouteName。你可以使用它。

如果你想在另一个模板中使用,你需要在控制器中定义它。

test.js(控制器)

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

test.hbs

{{appCtrl.currentRouteName}} 
+0

谢谢您的回答。我相关的知道ember.js,你会好心给我更多的解释如何在handlebars模板上使用currentRouteName?再次感谢。 – rafamds

+0

@rafaelmsantos'{{#如果currentRouteName = '登录'}!} {{标题栏}} {{/ if}个} {{出口}}' –

+0

这给了我一个生成错误:/ – rafamds

0

相同的答案的Ebrahim Pasbani。

这句法仍然在最新版本的灰烬可用:

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

其私有API,请不要使用它。 – kumkanillam