【问题标题】:Ember 2.1.0 render partial if index page else another partialEmber 2.1.0 渲染部分如果索引页面否则另一个部分
【发布时间】:2015-10-31 00:14:01
【问题描述】:

在我的 templates/application.hbs 中,我希望能够设置一个条件来检查它是否是我的索引页面,我为它呈现导航,否则它将为应用内页面呈现另一组导航。我的页脚也是如此。检查这个的最好方法是什么?此外,用户是否登录也无关紧要。

我的研究导致了过时的代码或约定。我对 Ember 也完全陌生,因此非常感谢任何帮助。

这是我的徽章尝试,但不起作用:

if eq currentRouteName="index" = partial 'partialName' else = partial 'partialName2'

第二次尝试成功:

第一次运行:ember install ember-truth-helpers

将此添加到我的应用程序模板中:

if (eq currentRouteName "index") = partial 'partialName' else = partial 'partialName2'

【问题讨论】:

    标签: ember.js ember-cli


    【解决方案1】:

    应用程序控制器有一个属性currentRouteName。您可以根据其值选择要呈现的导航。

    在你的application.hbs

    {{#if (eq currentRouteName "index")}}
      render navigation for your index page
    {{else}}
      render navigation for other pages
    {{/if}}
    

    eq 助手来自ember-truth-helpers

    【讨论】:

    • 获得了正确的标志语法,就像我在上面的原始帖子中的例子一样,一切都很好。谢谢!
    • 附带问题,如何使用 ember-truth-helper 检查两条不同的路线?例如,我想检查两条路线中的任何一条是否都不显示我的导航。下面的代码是我能想到的最好的格式,不需要太多指导:if (not-eq (eq currentRouteName "index" ) (eq currentRouteName "signup") )
    • 非常接近。您可能想要andor 而不是not-eq/xor。例如if (and (eq currentRouteName "index" ) (eq currentRouteName "signup") )。如果逻辑变得足够复杂,您应该在控制器/组件上创建一个属性。
    • 它不喜欢这样有两套,我的构建会破坏。还有什么我可能会丢失的吗?或者我如何在控制器或组件中编写它?
    • 您可以在应用程序控制器中放置一个计算属性。 shouldRenderNavigation: Ember.computed('currentRouteName', function() { let route = this.get('currentRouteName'); return route === 'index' || route === 'signup' })
    猜你喜欢
    • 1970-01-01
    • 2010-12-06
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 2017-05-03
    • 2014-07-18
    相关资源
    最近更新 更多