0

我在正确加载 snipcart 时遇到问题,它会在 localhost 上获取 api 键并显示购物车中的项目数,但是当部署到 netlify 时,它会在页面加载时短暂闪烁购物车中的项目数,指示 api密钥是正确的,但是它会出现错误 public API key not found. Attribute data-api-key must be set on #snipcart tag.

我正在使用 react 来渲染它,并将其降低到最小的样板文件中,以查看是否有任何 react 可能导致此问题,但我仍然遇到同样的问题。

移动 <div hidden id="snipcart" data-api-key={{ API_KEY }}></div>app.js文件并且它可以工作,但index.html它不起作用

索引.html

<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge"> 
    <link href ="https://fonts.googleapis.com/css2?family=Open+Sans&family=Lexend+Zetta&family=Poppins:wght@400;500;700&family=Shadows+Into+Light&display=swap" rel="stylesheet">
    <link rel="preconnect" href="https://app.snipcart.com">
    <link rel="preconnect" href="https://cdn.snipcart.com">
    <link rel="stylesheet" href="https://cdn.snipcart.com/themes/v3.0.23/default/snipcart.css" />
  </head>

  <body>
    <div id="root"></div>
    <script async src="https://cdn.snipcart.com/themes/v3.0.23/default/snipcart.js"></script>
    <div hidden id="snipcart" data-api-key={{ API_KEY }}></div>
  
    <script src="./index.tsx"></script>
    
  </body>

</html>
4

1 回答 1

3

把它放在你的 html 中不起作用,因为 {{API_KEY}} 是我从你的框架中假设的。由于 html 代码不是使用您的框架编译的,因此它不会对 <div hidden id="snipcart" data-api-key={{ API_KEY }}> 标签做任何事情。

要完成这项工作,请放置您的 api 密钥而不是 {{API_KEY}} 变量。

干杯,来自 Snipcart 的 Lea

于 2020-10-21T15:22:50.803 回答