未找到 amp-script 脚本哈希。 amp-脚本[脚本="hello-world"]

amp-script Script hash not found. amp-script[script="hello-world"]

我正在尝试使用 amp-script 但出现此错误:

"[amp-script] 未找到脚本哈希。amp-script[script="hello-world"].js 必须在 meta[name="amp-script-src"] 中具有 "sha384-BdjJFLYaZaVK-HgidJ2OFtpYsczYQc4N42NgKo7MOkF88iPbpdDWPgf86Y6PyEKO"。请参阅https://amp.dev/documentation/components/amp-script/#security-features.

error image

<!doctype html>
<html amp lang="en">
<head>
    <meta charset="utf-8">
    <meta class="trackPerformanceTag" content="AMP">
    <script src="https://cdn.ampproject.org/v0.js" async></script>
    <script async custom-element="amp-script" src="https://cdn.ampproject.org/v0/amp-script-0.1.js"></script>
    <meta name="amp-script-src" content="sha384-generated-sha">
    <title>title</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">

</head>
<body>

    <amp-script layout="container" script="hello-world" class="amp-script-sample">
        <button id="hello2">Click!</button>
    </amp-script>

    <script id="hello-world" type="text/plain" target="amp-script">
        const button = document.getElementById('hello2');
        button.addEventListener('click', () => {
            const h1 = document.createElement('h1');
            h1.textContent = 'Hello World 2!';
            document.body.appendChild(h1);
        });
    </script>

</body>
</html>

您需要复制开发工具控制台中错误消息中显示的完整哈希值。

在这种情况下;

sha384-BdjJFLYaZaVK-HgidJ2OFtpYsczYQc4N42NgKo7MOkF88iPbpdDWPgf86Y6PyEKO

并将其粘贴到 header 中的元数据:

<meta name="amp-script-src" content="PUT_THE_SHA_HERE">

每次您在脚本中更改某些内容时,都会生成新的哈希值,您需要再次 copy/paste 它。

See the screen with error form the dev tools console