【问题标题】:Why does this lodash template throw a syntax error? It looks fine to me为什么这个 lodash 模板会抛出语法错误?对我来说看起来不错
【发布时间】:2015-12-18 13:10:14
【问题描述】:

这个模板对我来说看起来非常好:

var renderFormElement = _.template([
    '<% if (input.hasOwnProperty("label")) { %>',
        '<label class="form__label" for="<%- input.key %>"> <%- input.label %> </label>',
    '<% } %>',
    '<% if (input.hasOwnProperty("options")) { %>',
        '<select name="<%- input.key %>" id="<%- input.key %>">',
            '<% _.forEach(input.options, function(option) { %>',
                '<option value=""><%- options %></option>',
            '<% }); %>',
        '</select>',
    '<% } elseif (input.hasOwnProperty("type")) { %>',
        '<input type="<%- input.type %>"/>',
    '<% } %>'
].join('\n'));

但它会引发“Uncaught SyntaxError: Unexpected token {”异常。我在这里疯了吗?不幸的是,我似乎无法为我的编辑器(Sublime Text)找到合适的 linter/syntax highlighter,并且将此代码粘贴到在线验证器中会给我完全相同的错误。

【问题讨论】:

  • 阅读How to Ask,按照步骤创建一个最小的、完整的、可验证的示例。如果您仍有问题,请发布。

标签: javascript templates syntax-error underscore.js lodash


【解决方案1】:

elseif 替换为else if..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    相关资源
    最近更新 更多