在Vue中使用HTML原生组件audio时,用this.$refs.audio
调用play
方法时报错,Property 'play' does not exist on type 'Vue[] | Element[]'.
,解决方法:
// 获取当前录音组件实例前先判断
getAudioInstance(): HTMLAudioElement {
if (this.$refs.audio instanceof Array) {
return this.$refs.audio[0] as HTMLAudioElement;
} else {
return this.$refs.audio as HTMLAudioElement;
}
}
// 调用audio相应API
const audio = this.getAudioInstance();
audio.play();