This repo is the types for the youtube api
https://www.npmjs.com/package/@types/youtube
What is the proper way to import these types into a vue2 project?
The following code complains as the type is not defined. But if i import the type ts complains and says missing dependency.
<template>
<div ref="ytplayer" class="MYouTube"></div>
</template>
<style lang="scss" scoped>
.MYouTube {
position: relative;
margin: 0 auto;
padding-bottom: 56.25%;
.iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 0;
}
}
</style>
<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import getYoutubeId from '@/utils/getYoutubeId';
import reframe from '@/utils/reframe';
import injectScript from '@/utils/injectScript';
@Component
export default class MYouTube extends Vue {
@Prop({ required: true })
url!: string;
vidId = '';
$refs!: {
ytplayer: any
};
created () {
const vidId = getYoutubeId(this.url);
if (vidId) {
this.vidId = vidId;
}
injectScript('https://www.youtube.com/iframe_api', () => {
this.injectAndPlay();
});
}
injectAndPlay () {
new YT.Player(this.$refs.ytplayer, {
videoId: this.vidId,
playerVars: {
'playsinline': 1
},
events: {
'onReady': (event) => {
event.target.playVideo();
reframe(this.$refs.ytplayer);
},
'onStateChange': () => {
console.log('do something');
}
}
});
}
}
</script>
I have a global.d.ts but in here I can only declare YT as any:
interface Window {
YT: any
}
In the tsconfig:
"typeRoots": [
"node_modules/@types"
],
but this does not auto resolve :/