knockout.js Magento 2.4.2版:无法处理绑定“ifnot:函数(){返回客户().全名}”消息:无法解析绑定

aor9mmx1  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(105)

在浏览器控制台中发现以下错误。它出现在我们升级电子商务网站运行的Magento应用程序后。

Uncaught SyntaxError: Unable to process binding "ifnot: function(){return customer().fullname }"
Message: Unable to parse bindings.
Bindings value: html:

我发现这个错误是由这一行引起的。

<!-- ko ifnot: customer().fullname  -->
<span class="not-logged-in" data-bind="html: '<?= $escaper->escapeHtmlAttr($welcomeMessage) ?>'"></span>
<?= $block->getBlockHtml('header.additional') ?>
<!-- /ko -->

下面是包含此代码的完整header.phtml文件

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * @var \Magento\Theme\Block\Html\Header $block
 * @var \Magento\Framework\Escaper $escaper
 */
$welcomeMessage = $block->getWelcome();
?>
<?php if ($block->getShowPart() == 'welcome') : ?>
    <li class="greet welcome" data-bind="scope: 'customer'">
        <!-- ko if: customer().fullname  -->
        <span class="logged-in"
              data-bind="text: new String('<?= $escaper->escapeHtml(__('Welcome, %1!', '%1')) ?>').replace('%1', customer().fullname)">
        </span>
        <!-- /ko -->
        <!-- ko ifnot: customer().fullname  -->
        <span class="not-logged-in" data-bind="html: '<?= $escaper->escapeHtmlAttr($welcomeMessage) ?>'"></span>
        <?= $block->getBlockHtml('header.additional') ?>
        <!-- /ko -->
    </li>
    <script type="text/x-magento-init">
    {
        "*": {
            "Magento_Ui/js/core/app": {
                "components": {
                    "customer": {
                        "component": "Magento_Customer/js/view/customer"
                    }
                }
            }
        }
    }
    </script>
<?php elseif ($block->getShowPart() == 'other') :?>
    <?= $block->getChildHtml() ?>
<?php endif ?>

下面是knockout.js中报告语法错误的代码部分。return new Function("$context", "$element", functionBody);行显示了红色标记

function createBindingsStringEvaluator(bindingsString, options) {
            // Build the source for a function that evaluates "expression"
            // For each scope variable, add an extra level of "with" nesting
            // Example result: with(sc1) { with(sc0) { return (expression) } }
            var rewrittenBindings = ko.expressionRewriting.preProcessBindings(bindingsString, options),
                functionBody = "with($context){with($data||{}){return{" + rewrittenBindings + "}}}";
            return new Function("$context", "$element", functionBody);
        }
qij5mzcb

qij5mzcb1#

我认为错误是来自这一行:

<span class="not-logged-in" data-bind="html: '<?= $escaper->escapeHtmlAttr($welcomeMessage) ?>'"></span>

您将html-to-bind Package 在额外的'标记中,请尝试移除它

相关问题