最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML5 Video element in vue.js? - Stack Overflow

programmeradmin3浏览0评论

I am trying to use the HTML5 Video element in a vue.js application. How can I wrap the video as a data property and use it in the vue.js app.

This is my code:

<template>
  <div class="video-player">
    <video class="video">
      <source src="@/assets/video.mp4" type="video/mp4">
    </video>
</template>

<script>
export default {
  name: "VideoPlayer",
  data: {
   video: null
  }
}
</script>

I am trying to use the HTML5 Video element in a vue.js application. How can I wrap the video as a data property and use it in the vue.js app.

This is my code:

<template>
  <div class="video-player">
    <video class="video">
      <source src="@/assets/video.mp4" type="video/mp4">
    </video>
</template>

<script>
export default {
  name: "VideoPlayer",
  data: {
   video: null
  }
}
</script>
Share Improve this question asked Dec 9, 2018 at 21:41 Samir AbaSamir Aba 531 gold badge1 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12

Firstly, you have forgotten a closing div tag.

The proper way to do this is by using refsand puted properties instead of data(). Just add a ref attribute to your video-tag

  <template>
      <div class="video-player">
        <video class="video" ref="video">
          <source src="@/assets/video.mp4" type="video/mp4">
        </video>
      </div>
    </template>
    
<script>
export default {
  name: "VideoPlayer",
   puted: {
    videoElement () {
      return this.$refs.video;
    }, 
  }
}
</script>

Then you can use your videoElement anywhere within the ponent by calling this.videoElement

Create ponent in ./ponents/Video.vue:

<template>
  <div class="video-wrap">
    <video controls="true">
      <source :src="mp4" type="video/mp4">
    </video>
  </div>
</template>
<script>
export default {
  name: "Video",
  props: {
    mp4: String
  }
}
</script>

Import to your ponent:

import Video from '@/ponents/Video.vue'

And use dynamic URL:

<Video :mp4="require(`@/assets/video.mp4`)" />
发布评论

评论列表(0)

  1. 暂无评论