我有如下的余烬帮助方法
export default () => {
Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
if (params[3]) { //handle case insensitive conditions if 4 param is passed.
params[0] = params[0].toLowerCase();
params[2] = params[2].toLowerCase();
}
let v1 = params[0];
let operator = params[1];
let v2 = params[2];
switch (operator) {
case '==':
return (v1 == v2);
case '!=':
return (v1 != v2);
case '===':
return (v1 === v2);
case '<':
return (v1 < v2);
case '<=':
return (v1 <= v2);
case '>':
return (v1 > v2);
case '>=':
return (v1 >= v2);
case '&&':
return !!(v1 && v2);
case '||':
return !!(v1 || v2);
default:
return false;
}
});
}
我正尝试从我的hbs文件中调用它,如下所示
{{#if (ifCond 'testing' '===' 'testing' true)}}
但是我遇到以下错误,有人可以向我提出一些建议,谢谢。
contacts.hbs:41 Uncaught Error: Module build failed (from ./.webpack/webpack-handlebars.js):
Error: if doesn't match each - 372:59
at validateClose (C:\SourceCode\IMS\Development\IMS\IMS.Ember\node_modules\ember-source\dist\ember-template-compiler.js:11014:13)
at Object.prepareBlock (C:\SourceCode\IMS\Development\IMS\IMS.Ember\node_modules\ember-source\dist\ember-template-compiler.js:11125:7)
at Object.anonymous (C:\SourceCode\IMS\Development\IMS\IMS.Ember\node_modules\ember-source\dist\ember-template-compiler.js:8339:25)
at Parser.parse (C:\SourceCode\IMS\Development\IMS\IMS.Ember\node_modules\ember-source\dist\ember-template-compiler.js:10142:38)
at parse (C:\SourceCode\IMS\Development\IMS\IMS.Ember\node_modules\ember-source\dist\ember-template-compiler.js:11271:36)
at preprocess (C:\SourceCode\IMS\Development\IMS\IMS.Ember\node_modules\ember-source\dist\ember-template-compiler.js:5817:71)
at precompile (C:\SourceCode\IMS\Development\IMS\IMS.Ember\node_modules\ember-source\dist\ember-template-compiler.js:3949:38)
at Object.precompile (C:\SourceCode\IMS\Development\IMS\IMS.Ember\node_modules\ember-source\dist\ember-template-compiler.js:8119:37)
at Object.module.exports (C:\SourceCode\IMS\Development\IMS\IMS.Ember\.webpack\webpack-handlebars.js:15:29)
at Object.<anonymous> (http://localhost:58799/Ember/dist/templates.js?version=1572992916177.89:22620:7)
答案 0 :(得分:0)
{{#
表示句柄期望``/}}`像这样:
{{#if true}}
do something
{{/if}}
但是,如果您将代码修改为
{{if (ifCond 'testing' '===' 'testing' true)}}
该错误将消失。