Angular 4无法在我的组件

时间:2017-07-12 09:35:12

标签: javascript angular typescript1.8 wavesurfer.js

我想在我的Angular项目上运行WaveSurfer.js并且它不能像我想的那样工作。 WaveSurfer在索引中表现完美。但是,我想在我的组件中使用WaveSurfer并且它不起作用。

索引(工作):

<!doctype html>
<html lang="en">
<head>
<script src="assets/js/wavesurfer.min.js"></script><
<script src="wavesurfer.microphone.min.js"></script>

<meta charset="utf-8">
<title>WaveAngular</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
<div id="waveform"></div>
<script src="assets/js/wavesurfer.js">
</script>
</script>
</body>
</html>

组件(没有工作):

<div class="dashboard-voice-parent">
<div class="dashboard-voice-Visualication">
<div id="waveform"></div>
<script src="assets/js/wavesurfer.js"></script>

 </div>
 <div class="dashboard-voice-buttoncontainer">
  <div class="dashboard-voice-buttonborder ">
 <button type="button" class="dashboard-voice-animated" 
  onclick="microphone.pause()" >
      <img src="/assets/svg/PauseBTN.png" >
    </button>
    <button type="button" class="dashboard-voice-VoiceBTN" 
    onclick="microphone.start()" >
      <img src="/asset`enter code here`s/svg/VoiceBTN.png">
    </button>
    <button type="button" class="dashboard-voice-animated" 
    onclick="microphone.stopDevice()" >
      <img src="/assets/svg/StopBTN.png" >
    </button>
    </div>
   </div>
   </div>

错误:

Uncaught ReferenceError: microphone is not defined
at HTMLButtonElement.onclick ((index):17)

这是我在JavaScripturire.js中的JavaScript代码

  var 
  wavesurfer=WaveSurfer.create({container:'#waveform',waveColor:'grey'});
  var microphone = Object.create(WaveSurfer.Microphone);

  microphone.init({
  wavesurfer: wavesurfer
  });

  microphone.on('deviceReady', function(stream) {
  console.log('Device ready!', stream);
  });
  microphone.on('deviceError', function(code) {
  console.warn('Device error: ' + code);
  });
  microphone.pause();
  microphone.play();  
  microphone.stopDevice();

以下是我使用的WaveSurfer:WaveSurfer.Microphone Example

我希望你们能帮我解决这个问题!

1 个答案:

答案 0 :(得分:1)

根据wavesurfer.js Overview,您可以尝试从npm安装waveurfer。

npm install --save wavesurfer.js@2.0.0-beta01

根据这个issue on the wavesurfer github page,可能存在一些问题,使得ES6导入能够正常使用webpack(Angular 4在引擎盖下使用)。该问题有一些关于如何将waveurfer及其插件导入组件的详细信息。

组件中的import语句可能如下所示:

import WaveSurfer from 'wavesurfer.js';
import Microphone from 'wavesurfer.js/dist/plugin/wavesurfer.microphone.min.js';

我建议尝试这种方法,并删除HTML中对waveurfer的任何<script>标记引用。它有点清洁。