Troubleshooting problems with integrating a Vue component (including video.js) into a Rails application

After integrating this Vue component into my Rails project, I encountered an issue.

<template>
 <div class="hello">
   <h1>THIS IS VUE</h1>

<video id="videok" class="video-js vjs-default-skin vjs-big-play-centered" 
 controls preload="auto" width="1500" height="700"
 poster="../../../assets/images/logo.png"
 data-setup='{"example_option":true}'>
<source src="../../../assets/videos/vid.mp4" type="video/mp4" />

</video>

</div>

</template>

<script lang="ts">

import { Component, Prop, Vue } from 'vue-property-decorator';
import videojs from 'video.js';


@Component
export default class VideoVue extends Vue {

 protected player: any;

 mounted() {

   this.player = videojs("videok");

}

}

</script>

<style scoped lang="css">
@import '../../src/video.js/dist/video-js.css';

</style>

To use this component in an .html.erb file, the following script needs to be added:

<%= javascript_pack_tag 'hello_vue' %>
<%= stylesheet_pack_tag 'hello_vue' %>

While the text "THIS IS VUE" is displaying correctly, a specific error has arisen.

Error in mounted hook: "TypeError: The element or ID supplied is not valid. (videojs)"

An observation reveals that

document.getElementById('videok')
returns as null when the mounted() function is called. Interestingly, if I create a new Vue project and paste this component code, it functions without any issues.

Answer №1

After checking out their documentation, I came across a completely different example here:

The process involves instantiating the Video.js player on mounted and then destroying it on beforeDestroy.

<template>
    <div>
        <video ref="videoPlayer" class="video-js"></video>
    </div>
</template>

<script>
import videojs from 'video.js';

export default {
    name: "VideoPlayer",
    props: {
        options: {
            type: Object,
            default() {
                return {};
            }
        }
    },
    data() {
        return {
            player: null
        }
    },
    mounted() {
        this.player = videojs(this.$refs.videoPlayer, this.options, function onPlayerReady() {
            console.log('onPlayerReady', this);
        })
    },
    beforeDestroy() {
        if (this.player) {
            this.player.dispose()
        }
    }
}
</script>

To use it properly, follow this example: (refer to the options guide for more information)

<template>
  <div>
        <video-player :options="videoOptions"/>
    </div>
</template>

<script>
import VideoPlayer from "@/components/VideoPlayer.vue";

export default {
    name: "VideoExample",
    components: {
        VideoPlayer
    },
    data() {
        return {
            videoOptions: {
                autoplay: true,
                controls: true,
                sources: [
                    {
                        src:
                            "/path/to/video.mp4",
                          type: "video/mp4"
                    }
                ]
            }
        };
    }
};

Remember to add the Video.js CSS file at video.js/dist/video-js.css.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to access Vue.js data within the Created() lifecycle hook

Is it possible to use data from Vue.js in my Created() function? I have some code to demonstrate why I'm asking. data (){ return { model: {}, foo: 'boo' } }, created (){ const ge ...

Tips for transmitting information from the main window to a child in JavaScript

Being relatively new to JavaScript, I am attempting to transfer a value entered into an input text field on a parent window to its child window when a button on the parent window is clicked. The parent window features an input field for entering a number a ...

The accuracy of Google Maps is compromised when the width is expanded

Here's a piece of code I use to adjust the size of the Google Map container: var myMap = document.getElementById('googleMap'); myMap.classList.toggle('fullscreen'); And here is the corresponding CSS: .fullscreen { width: 100% !i ...

Button will be disabled unless a value is selected in the dropdown menu

I am currently dealing with a code issue where the button is disabled on page load when the dropdown value is empty. However, even after selecting a value from the populated database dropdown, the button remains disabled. Jquery: <script> $(doc ...

Display a div in jQuery when a specific radio button is selected

I have set up three radio buttons to display three different divs, but the functionality is not working correctly. Below is the script I am using: <script> $(document).ready(function() { $("input[name$='Want_To']").click(function() { ...

Using JavaScript to set the value of a label within an ASP.NET ListView

Having trouble with an asp.net listview that contains two buttons, a textbox, and label in each row. When one of the buttons is clicked, a javascript function should be triggered. This function is supposed to take the value from the textbox and initiate a ...

The onClick function within the .map function is continuously triggered

I am encountering an issue with my code where a Dialog confirmation prompt from Material UI keeps getting called unexpectedly. The problem seems to arise when I add a value to the function that is triggered by a button click within a loop of an array usi ...

Is it possible to maintain HTML, JS, and CSS files as separate entities when developing Vue.js components, similar to how it is

Is it possible to maintain separate HTML, JS, and CSS files while creating Vue.js components? I recently read the "Why Vue.js doesn't support templateURL" article which discusses this topic. "Proper modularization is a necessity if you want to bu ...

Include a timer in the Promise.all and map function

I am seeking to enhance the interval between consecutive get requests below. The desired flow should appear as follows: timeout, https://example.com/example1, timeout, https://example.com/example2, timeout, https://example.com/example3, timeout, and so for ...

It appears that the JavaScript code for validating the form is not being executed

My task involves validating a form using JavaScript to display error messages for empty input fields. However, I am encountering an issue where the code does not trigger on submit. http://jsfiddle.net/LHaav/ Here is the HTML snippet: <head> ... ...

Can you pass just a single argument to a function that has two parameters?

Can I streamline my code by reducing redundancy? I have two functions in VueJS that are very similar, but one has two parameters while the other only has one. Is it possible to skip a parameter when calling a function in order to avoid redundancy? Here ar ...

Encountered an issue retrieving two rows nested within a parent row using Bootstrap 4 and VueJs

Currently, I am working on a VueJs and Bootstrap4 component where my aim is to achieve a design similar to the one shown using the available bootstrap classes. After going through the documentation, I came across the customized classes h-75 and h-25 provi ...

Get the value of a specific option within a React-bootstrap Form component

A special Form has been created to retrieve the selected value from an option. import Button from "react-bootstrap/Button"; import Form from "react-bootstrap/Form"; import { useState } from "react"; export default function Cu ...

Contrasting bracket notation property access with Pick utility in TypeScript

I have a layout similar to this export type CameraProps = Omit<React.HTMLProps<HTMLVideoElement>, "ref"> & { audio?: boolean; audioConstraints?: MediaStreamConstraints["audio"]; mirrored?: boolean; screenshotFormat?: "i ...

Traversing a multidimensional array with jQuery

In my quest to iterate through a multidimensional array, I encountered the need to remove the class "list" from the divs within the array. However, there could be multiple divs with this class on my site. My initial approach involved using two for-loops, ...

What is the process for adjusting the height of a scrollbar thumb?

Is it possible to set a fixed height for the scrollbar and adjust the amount of content scrolled accordingly with CSS? Here is my current CSS code: ::-webkit-scrollbar { width: 30px; } /* Track */ ::-webkit-scrollbar-track { box-shadow: inset ...

Tips for transferring information obtained from an API to my custom HTML page

As a novice in web development, I recently created a simple weather report website using the OpenWeather API. While I successfully fetched data from the API, I encountered an issue trying to display this data on my HTML page. Despite utilizing DOM manipu ...

Can Offer Codes be tested in TestFlight?

I'm currently working on an app with React Native Expo. My goal is to add a promo code feature that will extend users' free trial period for subscriptions. I've created a button within the app that directs users to this link https://apps.ap ...

"Must not be currently employed" when using window.open in a basic React application

Let me share a simplified version of the webapp I'm currently developing. Whenever I run into an Uncaught Error: Should not already be working. while executing the window.open(...) line in the following code snippet: const sleep = milliseconds => ...

The map pipe is malfunctioning

I am having trouble with displaying the key/value pairs of a map with a data type of <string, string[]>. Here is the custom pipe I am using: import { PipeTransform, Pipe } from "@angular/core"; @Pipe({ name: 'keys' }) export class KeysPi ...