模板中如何实现 “string === string”?

Handlebars 模板仅支持基本的布尔逻辑。您可以使用它来检查属性是否为 真值。

若要执行更复杂的逻辑,您需要在与模板关联的 Ember JS 文件中实现该逻辑。

例如,如果您有一个位于 templates/components/my-component.hbs 的模板,则必须使用 components/my-component.js 文件来进行计算。

在该文件中,您可以这样写:

import discourseComputed from "discourse-common/utils/decorators";
import Component from "@ember/component";

export default Component.extend({
  @discourseComputed("string1", "string2")
  property(string1, string2) {
    return string1 === string2;
  }
});

property 只是一个函数名称,您可以根据需要更改为任何名称。

然后在模板中,您可以这样写:

{{#if property}}
  blah blah...
{{/if}}

这将从 JS 文件中获取计算属性,并且仅当 property 返回 true 时,才显示 #if 块中的内容。