【发布时间】:2015-04-22 13:05:40
【问题描述】:
我正在尝试将一个参数从我的 HTMLBars 模板传递给 Helper。
根据文档,我创建了一个助手并明确注册了该助手:
export default Ember.HTMLBars.makeBoundHelper('is-foo', function(value, options) {
console.log("value: "+value);
});
但我收到一个错误“错误:断言失败:makeBoundHelper 生成的帮助程序不支持与块一起使用”
所以我尝试按照here 的建议使用 Ember.HTMLBars.helper 和 Ember.HTMLBars.registerHelper,但我收到错误“TypeError: Ember.default.HTMLBars.helper is not a function”
如果我没有明确地重新注册助手:
export default function(value, options) {
console.log("value: "+value);
};
然后我可以传递一个参数,但它没有得到解决并注销我传递的文字。
所以我尝试了here 概述的解决方案,但它似乎不适用于 CLI
我想要的结果是根据我发送给助手的参数值动态选择组件。我的 HTMLBars 代码如下所示:
{{#each foo in model}}
{{is-foo parameter}}
{{a-component}}
{{else}}
{{another-component}}
{{/is-foo}}
{{/each}}
我不确定下一步该做什么。任何帮助表示赞赏。
【问题讨论】:
-
在 CLI 中使用
ember g helper foo。它将为您创建一个 foo 助手。然后你可以查看它的代码,看看如何手动制作。 -
感谢您的回复!我使用 CLI 生成了一个助手,但它也创建了一个我不能与块一起使用的绑定助手。基本上我需要一种方法来有条件地从我的模板中选择一个组件,但我找不到任何方法让它工作。
-
请提供您的 HTMLBars 代码。
-
我已经用 HTMLBars 代码更新了问题。
-
因为:“我想要的结果是根据我发送给助手的参数值动态选择组件。” How to include a component from a controller in ember 的可能重复项