我想在React中使用javascript简单组件。
例如wavesurfer.js
如果你不使用react,它很容易使用。
<head>
<script src="https://unpkg.com/wavesurfer.js"></script>
</head>
<script>
let wavesurfer = WaveSurfer.create({
container: '#waveform',
waveColor: 'violet',
progressColor: 'purple'
});
</script>
<html>
<div id="waveform"></div>
</html>只有这段代码才能很好地工作。
所以,我尝试在React中做同样的事情。
我把<script src="https://unpkg.com/wavesurfer.js"></script>
在public/index.html中
然后上了课。
class Waveform extends Component {
componentDidMount(){
let wavesurfer = WaveSurfer.create({
container: '#waveform',
waveColor: 'violet',
progressColor: 'purple'
});}
render() {
return (
<div id="waveform"></div>
);
}
};但是,它显示错误
'WaveSurfer' is not defined no-undef
在我的理解中,wavesurfer.js是在head中从CDN中读取的
为什么找不到WaveSurfer类??
我的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://unpkg.com/wavesurfer.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>发布于 2021-01-09 05:31:02
我建议您改为执行var require:
Wavesurfer.js WaveSurfer =require(‘var’)
也许它可能比unpkg脚本更有效,因为unpkg脚本一定是破坏了包的提取,而谁又不能加载它。它可能只是一个错误,来自于无法执行的库加载。您可以使用node js或yarn来安装这个库。
npm install wavesurfer.js --save
# or
yarn add wavesurfer.js然后,只需导入该库并将其与可用变量配合使用即可:
import WaveSurfer from 'wavesurfer.js';
var WaveSurfer = require('wavesurfer.js');
define(['WaveSurfer'], function(WaveSurfer) {
// ... code
}); 如果这对你没有真正的帮助,请重新阅读下面的API站点,希望它能给你很大的帮助。https://wavesurfer-js.org/api/
发布于 2021-01-09 05:18:05
这看起来像是打字(可能是其他linter错误)。您需要禁用此行的no-undef规则。解析器不可能在设计/编译时知道这是在页面呈现时在运行时定义的。
发布于 2021-01-09 05:18:33
该错误来自eslint规则。在相关行// eslint-disable-next-line no-undef上添加注释,或将其添加到eslint配置中的全局变量。它没有破坏你的应用程序,只是破坏了线条。
https://stackoverflow.com/questions/65636288
复制相似问题