What is the best way to navigate to the next video in my YouTube API search results with Vue.js?

After setting up a search component that sends data to the parent player.component, I pass the video id and an object containing 5 search results to the player.component.

<template>
  <div class="mobile-screen">
    <b-container>
      <search-component
        v-on:updateVideoId="updateVideoId($event)"
        v-on:playlist="playlist($event)"
      />

      <youtube :video-id="videoId" ref="youtube" @playing="playing"></youtube>

      <button @click="playVideo">play</button>
      <button @click="stopVideo">stop</button>
      <button @click="nextVideo">next</button>
      <b-row>
        <b-col>
          <ul v-if="info">
            <li v-for="(item, index) in info.items" :key="index.etag">
              <p class="text">{{ item.snippet.videoId }}</p>
            </li>
          </ul>
        </b-col>
      </b-row>
    </b-container>
  </div>
</template>

I have access to both the videoId and the search result object. However, I am looking for a way to implement functionality where the player can play the next video in the search results list but unsure of how to achieve this.

<script>
import SearchComponent from "./SearchComponent.vue";
//import axios from "axios";
export default {
  components: { SearchComponent },
  name: "PlayerComponent",
  props: {
    msg: String,
  },
  data() {
    return {
      searchResult: null,
      videoId: "lG0Ys-2d4MA",
    };
  },
  mounted() {},
  methods: {
    updateVideoId(payload) {
      this.videoId = payload;
    },
    nextVideo() {},
    playlist(payload) {
      this.searchResult = payload;
      console.log("playlist", payload);
      this.player.nextVideo();
    },
    playVideo() {
      this.player.loadPlaylist({
        list: String,
        listType: String,
        index: Number,
        startSeconds: Number,
      });
      this.player.playVideo();
    },
    stopVideo() {
      this.player.stopVideo();
    },
    playing() {
      console.log(" we are watching!!!");
    },
  },
  computed: {
    player() {
      return this.$refs.youtube.player;
    },
  },
};
</script>

Answer №1

It appears that the searchResult value is not being utilized in this section. To rectify this, consider updating the videoId to correspond with the ID of the search result.

You could create a specific function for this task and make use of 'this.videoId = newId' to switch to the correct video.

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

Steps for choosing an option in MUI select list using jest

I am looking for a way to automate tests for a Material UI select component using Jest. Is there a way to select an option from the list of options in the Material UI component and confirm that its value has been successfully populated in Jest? I have se ...

Encountering a registration error persistently: [TypeError: Network request failed]

Currently, I am in the process of developing an application using React for the frontend and node.js for the backend. However, I have encountered a persistent network error whenever I try to sign up or log in. What puzzles me is that when I test the API en ...

Terminating the HTTP connection in Node.js after a set period of time

I am working on a program in nodejs that needs to retrieve json data from existing services. However, due to real time constraints in my application, I need to ensure that the response is prepared within a maximum of 2 seconds. If the response preparation ...

Assign Monday as the selected day of the week in the MUI Datepicker 6

I am currently using version 6 of the material ui Datepicker and I am trying to configure it so that the week starts on Monday. import React from "react"; import { DatePicker as DatePickerDestop } from "@mui/x-date-pickers/DatePicker"; ...

How to open a print preview in a new tab using Angular 4

Currently, I am attempting to implement print functionality in Angular 4. My goal is to have the print preview automatically open in a new tab along with the print popup window. I'm struggling to find a way to pass data from the parent window to the c ...

Callback function triggered upon the creation of a DOM node

I am in search of a way to have a specific callback function run each time a div that matches a particular selector is added to the DOM. I have explored the DOM events documentation and the event closest to what I need seems to be "load", however, it does ...

Using the function parameter as `this.variable` allows us to

I've been working with Vue.js and I'm currently trying to streamline my code by using one method for multiple purposes: data: { genders: [], months: [], } methods: { getModels: function(cat, type) { $.getJSON('/api/mode ...

Working with scrollIntoView in useEffect (Encountering an error trying to access 'scrollIntoView' property of null)

Starting from scratch, I'm working on setting up my webpage and crafting some code. function BasicDetails() { React.useEffect(()=>{ scrollView(); }, []); function scrollView() { document.getElementById('main-root& ...

We encountered an error: The function setName is not defined

I am working on some code where I have defined a function, but it is still showing me errors related to setname, setemail, and password. import React, {useState} from 'react' import './Auth.css' import icon from '../../assets/logo. ...

Can the .scroll function be turned off when a user clicks on an anchor link that causes the page to scroll?

I am currently developing a timeline page and I want to implement a feature similar to the chronological list of years displayed on the right side of this webpage: As part of this feature, I have set up a click event which adds a circle border around the ...

Effective strategies for minimizing the bundle size of your NextJs application

Recently, I launched my first NextJS app and was surprised to see that the initial bundle size is around 1.5Mb, which seems quite large for me as a beginner in using Nextjs. I have shared an image of the yarn build and also my package.json. All the pages ...

What could be the reason for my dynamic image not appearing in a child component when using server-side rendering in Nuxt and Quasar

Currently, I am tackling SSR projects using Nuxt and Quasar. However, I encountered an issue when trying to display a dynamic image in a child component as the image is not being shown. The snippet of my code in question is as follows: function getUrl (im ...

javascriptJQuery validation function triggers JSF ajax request

I am currently experiencing a challenge with JSF ajax requests and jQuery events. I am using a jQuery event to validate a section of a form like this: jQuery(".btnNextStep").live("click", function(e) { var error = !validate(id); ... ...

When transferring JSON to JavaScript within Laravel, the JSON data gets converted into HTML entities by JavaScript

Within my Laravel controller, I am constructing a multidimensional associative array: $trendChart = Array ( "series" => Array ( "data" => Array(1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 5) ), "xaxis" => Arr ...

When using Jquery, input can be appended on the last child element when a key

I am new to JavaScript and want to enhance my page's functionality using jQuery. Here is my current HTML: <div class="input_fields_wrap"> <div><ul class="inputList"></ul></div> </div> My goal ...

Having trouble disabling an HTML select menu using JQuery?

My HTML page includes multiple select menus that all have the shared class name .iconDropDownMenu. During generation with PHP, some of these select menus may be hidden by PHP adding an additional class. I attempted to disable only the hidden .iconDropDown ...

Retrieve the input field value using JavaScript within a PHP iteration loop

I've implemented an input box inside a while loop to display items from the database as IDs like 001,002,003,004,005 and so on. Each input box is accompanied by a button beneath it. My Desired Outcome 1) Upon clicking a button, I expect JavaScript t ...

How to extract styles from a string using PHP

There is a string that contains some inline styles within <P><span><font> tags <p style='color:red; font-size:21px'><span style='color:blue;'><font style='font-family:Arial'>This is a ...

Avoid Scroll Below Stuck Navigation

I've been searching for a solution to my issue for some time now, and while I've come across many articles discussing similar problems, none of them seem to address my specific problem. In my React app, I have a mobile version where users can ta ...

Troubleshooting a resizing problem in Angular's ag-grid

Is there a way to enable column resizing without allowing users to resize columns to the left side and create blank spaces on the right of the grid? Please refer to the image below for clarification. https://i.sstatic.net/cnhYn.png ...