Vue.js 通过 URL 播放视频:详细步骤和示例
Vue.js 本身不提供视频播放功能,但你可以使用 HTML5 的 <'video'> 标签来播放视频。以下是如何在 Vue.js 中通过 URL 参数播放视频的详细步骤:
1. 在 Vue 组件中添加 <'video'> 标签
<template>
<div>
<video ref="videoPlayer" width="640" height="360">
<source :src="videoUrl" type="video/mp4">
</video>
</div>
</template>
2. 在 Vue 实例中定义 videoUrl 变量并获取 URL 参数
<script>
export default {
data() {
return {
videoUrl: ''
}
},
created() {
const searchParams = new URLSearchParams(window.location.search)
this.videoUrl = searchParams.get('url')
}
}
</script>
3. 在 mounted 生命周期中调用 play 方法播放视频
<script>
export default {
mounted() {
this.$refs.videoPlayer.play()
}
}
</script>
4. 在 URL 中传递视频 URL 参数
http://example.com?url=http://example.com/video.mp4
通过以上步骤,你可以通过 URL 来播放视频。为了确保浏览器兼容性,建议同时提供多种视频格式的源文件。
原文地址: https://www.cveoy.top/t/topic/n4Rh 著作权归作者所有。请勿转载和采集!