javascript - 如何在 React 中使用 CDN javascript

标签 javascript reactjs

我想在 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从CDN读取head

为什么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>

最佳答案

我建议你改为执行 var require :

var WaveSurfer = require('wavesurfer.js')

也许它比 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/

关于javascript - 如何在 React 中使用 CDN javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65636288/

相关文章:

javascript - 下一个JS : async getInitialProps() with AWS S3?

javascript - HTML 中的嵌套引用

javascript - 名称列表取决于对象中的另一个字段

javascript - jQuery - 如果用户选择了下拉项,则删除该下拉项

Javascript 'this' 关键字返回 href 属性而不是 anchor 标记上的对象

javascript - 动态 HTML 字符串来 react 组件

javascript - React.js 和 ES6 : Any reason not to bind a function in the constructor

reactjs - React Slick prev 和 next 方法抛出错误

c# - 服务器端代码中的 HTML 输入值。网站

javascript - 在 React 应用程序中包含样式