下面是React组件调用外部vanilla aws lex javascript进行对话。呼叫新的LexAudio.conversation(...)带来了问题
 将外部js库导入react组件的最佳方法是什么?

class MyComponent extends Component {

  startConversation() {
    AWS.config.credentials =  new AWS.Credentials('', '', null);
      AWS.config.region = 'us-east-1';
      var waveform = require('./renderer_1.js');
      var waveform = window.Waveform();
      var config = {
          lexConfig: { botName: 'testbot' }
      };
      var message = document.getElementById('message');
      var LexAudio = require('./aws-lex-audio.js');
      var conversation =  new LexAudio.conversation(config, function (state) {

          message.textContent = state + '...';
          if (state === 'Listening') {
              waveform.prepCanvas();
          }
          if (state === 'Sending') {
              waveform.clearCanvas();
          }
      }, function (data) {
          console.log('Transcript: ', data.inputTranscript, ", Response: ", data.message);
      }, function (error) {
          //message.textContent = error;
      }, function (timeDomain, bufferLength) {
          waveform.visualizeAudioBuffer(timeDomain, bufferLength);
      });
      conversation.advanceConversation();
  }
  render() {
    return (
      <div className='Hello'>`enter code here`
        <meta charSet="UTF-8" />
        <link rel="stylesheet" href="style.css" />
        <div className="audio-control" onClick={this.startConversation}>
          <p id="audio-control" className="white-circle">
            <img  alt="new" src="./lex.png" />
            <canvas className="visualizer" />
          </p>
          <p><span id="message" /></p>
        </div>
      </div>
    );
  };
}

export default MyComponent;


感谢您的帮助。

最佳答案

尝试在页面顶部导入LexAudio。您的大部分进口商品都应该运到这里。

就像是:

import LexAudio from './aws-lex-audio.js';

代替:

var LexAudio = require('./aws-lex-audio.js');

关于javascript - 错误“LexAudio.conversation不是构造函数”。 React组件调用Vanilla JavaScript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54845868/

10-11 06:07