Javascript plotly在react中未定义
我正在react中创建一个天气应用程序,它使用api在图形中显示天气预测,但我得到一个错误plotly未定义,尽管我已在index.html中粘贴了plotly的cdnJavascript plotly在react中未定义,javascript,reactjs,Javascript,Reactjs,我正在react中创建一个天气应用程序,它使用api在图形中显示天气预测,但我得到一个错误plotly未定义,尽管我已在index.html中粘贴了plotly的cdn <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<!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" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
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" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
因为它在全局范围内未定义,所以请尝试在组件外部控制Plotly对象。 如果您使用Webpack进行捆绑,请尝试将Plotly安装为NPM包。
然后使用ES6导入将其导入到项目中。如果在全局范围内未定义,请通过npm进行绘图安装 npm安装plotly.js 并将其导入到您的as中
import Plotly from 'plotly.js';
我在我的文件夹中使用npm install plotly命令安装了plotly,但它仍然给我相同的错误。您也可以尝试使用react plotly