Skip to content

athiwatp/vue-youtube-embed

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vue YouTube Embed

This is a component for Vue.js to utilize YouTube iframe API easily. This is based on Angular YouTube Embed

License

MIT License

install

<script src="vue-youtube-embed.js"></script>
<script>
Vue.use(VueYouTubeEmbed)
</script>

or

npm install --save vue-youtube-embed
'use strict'
import Vue from 'vue'
import VueYouTubeEmbed from 'vue-youtube-embed'
Vue.use(VueYouTubeEmbed)

Requirement

  • Vue.js

Usage

Please pass the ID of the video that you'd like to show.

<youtube :video-id="videoId"></youtube>

Props

These are available props.

  • player-width: String, default value is 640
  • player-height: String, default value is 390
  • player-vars: Object, default value is {start: 0, autoplay: 0}
  • video-id: String, required

Methods

These functions are the same as the original one.

  • getIdFromURL
  • getTimeFromURL
let videoId = VueYouTubeEmbed.getIdFromURL(url)
let startTime = VueYouTubeEmbed.getTimeFromURL(url)

or

export default {
  methods: {
    method(url) {
      this.videoId = this.$youtube.getIdFromURL(url)
      this.startTime = this.$youtube.getTimeFromURL(url)
    }
  }
}

Events

These are the events that will be emitted by the component.

  • ready
  • ended
  • playing
  • paused
  • buffering
  • queued
  • error

The first argument is an instance of YT.Player.

Example

<div id="#app">
  <section>
    <h2>listening events</h2>
    <youtube :video-id="videoId" @ready="ready" @playing="playing"></youtube>
  </section>
  <section>
    <h2>add options</h2>
    <youtube :video-id="videoId" player-width="1280" player-height="750" :player-vars="{autoplay: 1}"></youtube>
  </section>
</div>
'use strict'
import Vue from 'vue'
import VueYouTubeEmbed from 'vue-youtube-embed'

Vue.use(VueYouTubeEmbed)

const app = new Vue({
  el: '#app',
  data: {
    videoId: 'videoId',
  },
  methods: {
    ready(player) {
      this.player = player
    },
    playing(player) {
      // The player is playing a video.
    },
    change() {
      // when you change the value, the player will also change.
      // If you would like to change `playerVars`, please change it before you change `videoId`.
      // If `playerVars.autoplay` is 1, `loadVideoById` will be called.
      // If `playerVars.autoplay` is 0, `cueVideoById` will be called.
      this.videoId = 'another video id'
    },
    stop() {
      this.player.stopVideo()
    },
    pause() {
      this.player.pauseVideo()
    }
  }
})

Contribution

  • contribution welcome!

Packages

No packages published

Languages

  • JavaScript 93.3%
  • HTML 6.7%