Javascript 未捕获的(承诺中的)异常不能';我好几个小时都应付不来
我正在尝试使用动态导入制作drum kit,但动态导入返回了一个承诺。我正在将音频字符串从容器组件发送到DrumKit.js。我尝试了解决此问题的所有方法,但我无法处理它Javascript 未捕获的(承诺中的)异常不能';我好几个小时都应付不来,javascript,reactjs,Javascript,Reactjs,我正在尝试使用动态导入制作drum kit,但动态导入返回了一个承诺。我正在将音频字符串从容器组件发送到DrumKit.js。我尝试了解决此问题的所有方法,但我无法处理它 import '../styles/DrumKit.css' import React from 'react' class DrumKit extends React.Component{ constructor(props){ super(props); this.playSound=this.play
import '../styles/DrumKit.css'
import React from 'react'
class DrumKit extends React.Component{
constructor(props){
super(props);
this.playSound=this.playSound.bind(this);//We need different methods
for each of drumkits.
this.endSound=this.endSound.bind(this);
this.state={
playing:false,
className:'simpleButton',
};
window.addEventListener('keydown',this.playSound);
this.audio=new Audio(import(`../sounds/${this.props.audio}.wav`));
}
endSound(){
this.setState({
playing:false
})
}
playSound(event){
if(event.keyCode===this.props.code||event.type==='click') {
if(this.audio!==undefined){
this.audio.play()
}
}
else{
return ;
}
this.setState({
playing:true
});
}
render(){
const playing=this.state.playing? "isRinging" : "";
return (
<div>
<button
onTransitionEnd={this.endSo"und}
className={this.state.className}
id={playing}
onClick={this.playSound}>
<kbd>{this.props.letter}</kbd>
<span className='sound'>{this.props.ring}</span>
</button>
</div>
)
}
js:25 Uncaught(in promise)DOMException
import
希望加载一个JavaScript模块,而不是.wav
文件。使用一个普通的XHR或fetch
API。我是React的新手,我的导师希望这样做。那么你可能应该问你的导师这应该如何工作。可能您使用的模块加载器也支持其他格式,我刚刚注意到您还导入了CSS文件。在任何情况下,这可能是DOM异常的原因,新音频
构造函数没有将承诺作为其参数。相反,写audioPromise=import(…)。然后(data=>newaudio(data))代码>。还请注意,您可能在React中使用了componentDidMount
hook,并且您需要关心加载期间或加载失败时应该发生什么;我犯了一个错误,它说不能将对象转换为原始值
index.js:1509 Uncaught Error: The error you provided does not contain
a stack trace.
at B (index.js:1509)
at G (index.js:1816)
at index.js:1831
at index.js:1850
at index.js:1343
B @ index.js:1509
G @ index.js:1816
(anonymous) @ index.js:1831
(anonymous) @ index.js:1850
(anonymous) @ index.js:1343