如何使用vuejs和@vueuse/sound播放不同的声音效果?

elcex8rz  于 2023-05-01  发布在  Vue.js
关注(0)|答案(1)|浏览(285)

我很难理解如何将声音效果的名称作为参数传递给设置中设置的播放常量,从而播放声音效果“button2”而不是“buttonSFX”。

<script> import { useSound } from '@vueuse/sound' 
import buttonSfx from '../assets/button.mp3'
import button2 from '../assets/button2.mp3'

export default {   
setup() {
    const { play } = useSound(buttonSfx)
    return {
      play,
    }   
  }, 
} 
</script>
ql3eal8s

ql3eal8s1#

下面是我如何让它工作的

<script setup>
import { useSound } from '@vueuse/sound';
import plusSound from '@/assets/sounds/click-sound.mp3';
import minusSound from '@/assets/sounds/minus-click.mp3';

const plus = useSound(plusSound);
const minus = useSound(minusSound);

const increment = () => {
  plus.play()
 .....
}

const decrement = () => {
  minus.play()
 .....
}

</script>

相关问题