错误“ LexAudio.conversation不是构造函数”。 React组件调用Vanilla JavaScript

时间:2019-02-23 20:26:22

标签: javascript reactjs amazon-web-services react-native amazon-lex

下面是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;

谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

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

类似的东西:

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

代替:

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