更新
有朋友评论说不行,今天有机会又试了一次,测试了好像没啥问题呀,新的代码:
<script setup lang="ts">
import {getCurrentInstance, onMounted, ref} from "vue";
function loop(){
console.log(player.value)
}
const player = ref<HTMLAudioElement>();
</script>
<template>
<div>
<button @click="loop"></button>
<audio ref='player' id="music" loop="true" autoplay src="https://web.opendrive.com/api/v1/download/file.json/OV84MDUyNTM1NF9aSXB6Sw?inline=1"></audio>
</div>
</template>
注意哦: 用的Vue3 + Typescript
实例:
<script setup lang="ts">
import {getCurrentInstance, onMounted, ref} from "vue";
function loop(){
console.log(playerRef.value)
}
const playerRef = ref<HTMLAudioElement>();
</script>
<template>
<div>
<button @click="loop"></button>
<audio ref='playerRef' id="music" loop="true" autoplay src="https://web.opendrive.com/api/v1/download/file.json/OV84MDUyNTM1NF9aSXB6Sw?inline=1"></audio>
</div>
</template>
说明:
变量名称和元素的ref属性值相同,即例子中的两个threeDom
.