javascript - “未定义条带”错误 - 在加载脚本之前调用函数?
问题描述
我认为我的脚本无法加载(或加载过早)有问题。
我尝试为支付实现Stripe 库。为此,在我的代码中使用 Stripe 函数之前,我必须包含一个脚本。我在我的公共文件夹中的标头 index.html 中添加了给定的脚本 src="https://js.stripe.com/v3/",如下所示:
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<script src="https://js.stripe.com/v3/"></script>
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
我创建了一个空应用程序,只调用了 Stripe。这是 App.js :
import React from 'react';
import './App.css';
import {Button} from 'react-bootstrap'
class App extends React.Component {
constructor() {
super();
this.handleClick = this.handleClick.bind(this);
}
handleClick(){
var stripe = Stripe('pk_test_XXXXXXXXXXXX');
// I will use stripe variable later. But for now I have an error with previous line
}
render() {
return(
<div>
<Button
className= "btn-xlBook"
variant="primary"
onClick={this.handleClick}>
OK
</Button>
</div>
)
}
}
export default App
但是当我启动纱线时,我有错误'Stripe' is not defined no-undef。
我不明白,我在标题中调用脚本,但行为就像尚未加载脚本一样。
你知道怎么解决吗?II 在 index.html 的头文件中添加一个脚本,它应该在应用程序之前加载,不是吗?
解决方案
您不会Stripe
在任何地方导入。就像你做Button
的那样。查看React Stripe Elements,了解如何在 React 中使用 Stripe 的一个示例。
推荐阅读
- c# - 如何解决:“单声道版本不匹配”
- javascript - 如何使用 vuejs 指令或组件优雅地编写此代码?
- javascript - 具有对象数组和对象到对象的通用映射函数
- r - 如何在R中保存复杂列表的结构
- git - Travis-ci:.travis.yml 中每个分支的环境变量
- java - 为什么我的协程异步只能在 runBlocking 内工作?
- sql - VB.NET:数据列日期时间设置 NullValue
- java - 动态注入 Google Guice bean
- javascript - 如何通过在 svg 中保存开始时间来重新启动动画
- xamarin.forms - 未设置搜索/查找 FindAccountsForServiceAsync 对象引用