【问题标题】:How to pass conditional string to ember component attribute using concat?如何使用 concat 将条件字符串传递给 ember 组件属性?
【发布时间】:2020-12-18 09:31:59
【问题描述】:

使用emberconcat()方法赋值组件属性时,传递条件字符串的正确方法是什么?

考虑以下

{{#my-compontent class=(concat (dasherize model.name)"-row")}}
  {{model.name}}
{{/my-component}}

我想要做的是基于model.disabled 属性有条件地添加--disabled 后缀。

我尝试过的:

{{#my-compontent class=(concat (dasherize model.name)"-row{{if model.disabled '--disabled'}}")}}
  {{model.name}}
{{/my-component}}

导致:

<div class="component-name-row{{if model.disabled '--disabled}}">
  Component name
</div>

我尝试的另一件事是使用ternary 助手,基于this answer

{{#my-compontent class=(concat (dasherize model.name)"-row"(ternary model.disabled '--disabled' ''))}}
  {{model.name}}
{{/my-component}}

但它崩溃了:

断言失败:找不到名为“三元”的助手

有没有办法做到这一点?如果不是,我将如何使用不同的方法达到相同的效果?

【问题讨论】:

    标签: javascript ember.js handlebars.js


    【解决方案1】:

    像往常一样,问了5分钟后我自己找到了答案:

    {{#my-compontent class=(concat (dasherize model.name)"-row(if model.disabled '--disabled')")}}
      {{model.name}}
    {{/my-component}}
    

    线索是使用(if condition 'string') 而不是{{if condition 'string'}}

    【讨论】:

    • 如果您觉得它更具可读性,您可以使用(concat (dasherize appName) "-row" (if true '--disabled'))
    • 您可能还会觉得这个提议很有趣:github.com/emberjs/rfcs/issues/623
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2020-09-09
    相关资源
    最近更新 更多