将脚本放入 public/index.html 后,Tyro 未定义问题

问题描述 投票:0回答:1

我正在尝试将 Tyro 支付网关实现到我的 React 应用程序中。 (链接:https://preview.redoc.ly/tyro-connect/PLA-4230/app/apis/pay/accept-an-online- payment/

当我尝试运行我的应用程序时,它说

Tyro()
未定义。我已将脚本标签放入public/index.html,但它不起作用。

错误是:

[Eslint]“Tyro 未定义(no-undef)”

下面是我的 public/index.html 文件:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
    <script src="https://pay.connect.tyro.com/v1/tyro.js"></script>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  </body>

</html>

还有我的 Tyro 支付组件:

import React, { useEffect, useState } from 'react';

function TyroPayment() {
  const [tyroLibraryLoaded, setTyroLibraryLoaded] = useState(false);
  useEffect(() => {
    if (tyroLibraryLoaded === true) {
      init();
    }
  }, [tyroLibraryLoaded]);

  useEffect(() => {
    const script = document.createElement('script');
    script.src = 'https://pay.connect.tyro.com/v1/tyro.js';
    script.corssOrigin = 'anonymous';
    script.async = true;
    script.onload = () => setTyroLibraryLoaded(true);
    document.body.appendChild(script);
  }, []);

  let paySecret;
  // JavaScript sample code
  async function init() {
    const tyro = Tyro();
    /*
    const response = await fetch('/create-order', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ apple: 55 }),
    });
    paySecret = await response.json().paySecret;
     */
    const paySecret = '$2a$10$KvfnNafAQguNViSHxLZsVuXv9ITJ56rCVFGIzDencnSB81TpvgAgm';
    await tyro.init(paySecret);

    const payForm = tyro.createPayForm();
    payForm.inject('#tyro-pay-form');
  }

  return (
    <div className='mainpage-container'>
      <form id='pay-form'>
        <div id='tyro-pay-form'></div>
        <button id='pay-form-submit'>Pay</button>
      </form>
    </div>
  );
}

export default TyroPayment;

如何使用 script 标签实例化

Tyro()
,就像 React 文档中的步骤 3 一样?

我暂时将

paySecret
设置为字符串以避免 API 调用。

javascript html reactjs
1个回答
3
投票

我用谷歌搜索,发现只需在组件顶部添加这一行即可解决它:

/* eslint no-undef: 0 */ // --> OFF

现在组件运行没有任何问题了:)

© www.soinside.com 2019 - 2024. All rights reserved.