如何将HTML +JavaScript引入TypeScript(在.tsx文件中嵌入Shopify购买按钮)

bogh5gae  于 2022-11-18  发布在  TypeScript
关注(0)|答案(1)|浏览(136)

尝试将Shopify整合到我的个人网站。
前端是React(NextJS w/ TypeScript)。Shopify buy按钮的嵌入代码是一个HTML div标签 Package JavaScript。我如何才能成功地将代码引入我的.tsx文件以在我的前端上使用?

**代码:**以下是Shopify提供的代码,用于在HTML和JS中的.html页面上嵌入Buy按钮:

<div id='collection-component-XXXXXXXXXX9'></div>
<script type="text/javascript">
/*<![CDATA[*/
(function () {
  var scriptURL = 'https://sdks.shopifycdn.com/buy-button/latest/buy-button-storefront.min.js';
  if (window.ShopifyBuy) {
    if (window.ShopifyBuy.UI) {
      ShopifyBuyInit();
    } else {
      loadScript();
    }
  } else {
    loadScript();
  }
  function loadScript() {
    var script = document.createElement('script');
    script.async = true;
    script.src = scriptURL;
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(script);
    script.onload = ShopifyBuyInit;
  }
  function ShopifyBuyInit() {
    var client = ShopifyBuy.buildClient({
      domain: 'xxxxxxxxxxx.myshopify.com',
      storefrontAccessToken: 'xxxxxxxxxxxxxxxxxxxxxx',
    });
    ShopifyBuy.UI.onReady(client).then(function (ui) {
      ui.createComponent('collection', {
        id: 'xxxxxxxxxxxxx',
        node: document.getElementById('collection-component-XXXXXXXXXXX9'),
        moneyFormat: '%24%7B%7Bamount%7D%7D',
        options: {
  "product": {
    "styles": {
      "product": {
        "@media (min-width: 601px)": {
          "max-width": "calc(25% - 20px)",
          "margin-left": "20px",
          "margin-bottom": "50px",
          "width": "calc(25% - 20px)"
        },
        "img": {
          "height": "calc(100% - 15px)",
          "position": "absolute",
          "left": "0",
          "right": "0",
          "top": "0"
        },
        "imgWrapper": {
          "padding-top": "calc(75% + 15px)",
          "position": "relative",
          "height": "0"
        }
      },
      "title": {
        "font-size": "14px",
        "color": "#000000"
      },
      "button": {
        ":hover": {
          "background-color": "#10002b"
        },
        "background-color": "#100030",
        ":focus": {
          "background-color": "#10452b"
        },
        "padding-left": "35px",
        "padding-right": "35px"
      },
      "price": {
        "color": "#086cce"
      },
      "compareAt": {
        "color": "#0000ce"
      },
      "unitPrice": {
        "color": "#0000ce"
      }
    },
    "text": {
      "button": "Add to cart"
    }
  },
  "productSet": {
    "styles": {
      "products": {
        "@media (min-width: 601px)": {
          "margin-left": "-20px"
        }
      }
    }
  },
  "modalProduct": {
    "contents": {
      "img": false,
      "imgWithCarousel": true,
      "button": false,
      "buttonWithQuantity": true
    },
    "styles": {
      "product": {
        "@media (min-width: 601px)": {
          "max-width": "100%",
          "margin-left": "0px",
          "margin-bottom": "0px"
        }
      },
      "button": {
        ":hover": {
          "background-color": "#10452b"
        },
        "background-color": "#124d30",
        ":focus": {
          "background-color": "#10452b"
        },
        "padding-left": "35px",
        "padding-right": "35px"
      },
      "title": {
        "font-family": "Helvetica Neue, sans-serif",
        "font-weight": "bold",
        "font-size": "26px",
        "color": "#40004c"
      },
      "price": {
        "font-family": "Helvetica Neue, sans-serif",
        "font-weight": "normal",
        "font-size": "14px",
        "color": "#40004c"
      },
      "compareAt": {
        "font-family": "Helvetica Neue, sans-serif",
        "font-weight": "normal",
        "font-size": "11.9px",
        "color": "#40004c"
      },
      "unitPrice": {
        "font-family": "Helvetica Neue, sans-serif",
        "font-weight": "normal",
        "font-size": "11.9px",
        "color": "#40004c"
      }
    },
    "text": {
      "button": "Add to cart"
    }
  },
  "option": {},
  "cart": {
    "styles": {
      "button": {
        ":hover": {
          "background-color": "#10002b"
        },
        "background-color": "#100000",
        ":focus": {
          "background-color": "#10002b"
        }
      }
    },
    "text": {
      "total": "Subtotal",
      "button": "Checkout"
    },
    "popup": false
  },
  "toggle": {
    "styles": {
      "toggle": {
        "background-color": "#120030",
        ":hover": {
          "background-color": "#10002b"
        },
        ":focus": {
          "background-color": "#10002b"
        }
      }
    }
  }
},
      });
    });
  }
})();
/*]]>*/
</script>
9gm1akwq

9gm1akwq1#

我可能会将整个<script>放在index.html中,然后将<div id='collection-component-XXXXXXXXXX9'></div>添加到希望嵌入组件中。

相关问题