Ways to transfer information from a function within one element to another element

I have two components: one that logs the indexes of a carousel and I need to pass these indexes into the second component.

First Component

<template>
  <div class="container container--white">
    <Header />
    <carousel-3d
      @after-slide-change="onAfterSlideChange"
      :width="250"
      :height="300"
      class="main__carousel"
    >
      <slide v-for="(img, i) in images" :key="i" :index="i">
        <img :src="img.src" />
      </slide>
    </carousel-3d>
    <CircleBottom v-bind:images="images" />
  </div>
</template>

export default {
.
.
.
  methods: {
    onAfterSlideChange(index) {
      // eslint-disable-next-line no-console
      console.log(index);
    }
  }
};
</script>

Second Component - Where index needs to be placed:

:src="articles[{{index}}].img"

<template>
  <div class="main__bottom-articles">
    <article class="articles__view">
      <div class="articles__view-top">
        <button @click="onCloseClick">-</button>
        <h1>{{articles[1].title}}</h1>
        <p>{{articles[1].desc}}</p>
      </div>
      <div class="articles__view-bottom">
        <img :src="articles[1].img" width="340px" height="auto" />
      </div>
    </article>
  </div>
</template>

Answer №1

Sharing information with child elements is clearly outlined in the following resource: https://v2.vuejs.org/v2/guide/components.html

To put it simply:

<parent>
  <componentx :index="index">
    <component2 :index="index" />
  </componentx>
</parent>

The parent component only requires this code inside the component:

data: {
  index: 0
},
methods: {
   onAfterSlideChange(index) {
       this.index = index;
   }
}

And for componentx and component2:

props: ['index'],

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

Are you looking for a way to prevent the default behavior of an event in angular-ui-router 1.0.x? Check out

Recently, I made a change in my code where I replaced $stateChangeStart with $transitions.onStart $rootScope.$on('$stateChangeStart', function(e, ...){ e.preventDefault(); // other code goes here... }); Now, the updated code looks lik ...

Ways to showcase additional content

When I receive a JSON Object containing posts from a WordPress account, it only includes about 15 posts. What steps can I take to retrieve more than that amount? The structure of the JSON data is as follows: { ID: 4164, title: "24 Hour Non-Stop with Ma ...

Using a Node.js module to shrink HTML files

Is there a way to minify HTML before rendering it? I'm aware of console minifiers like: html-minifier However, I am looking for a solution that can be implemented in the code itself. Something along the lines of: var minifier = require('some- ...

Recognizing when a Bootstrap dropdown with multiple options is deselected through Jquery

I'm working with a dynamic bootstrap multiple drop-down selector and I need to execute some code when a user deselects an option. <select id="selectOptions" multiple> <option>Test1</option> <option>Test2</option> & ...

It's next to impossible to secure expedited work on an ongoing project using Vercel

Yesterday, I successfully deployed an application on Vercel using only ReactJS. Today, I made the decision to develop an API for my application, To clarify, I have a folder housing the React app, and within that, I created a directory named "api" followi ...

What is the best way to populate a div with multiple other div elements?

My current project involves creating a sketchpad using jQuery for an Odin Project assignment. However, I have encountered an issue with filling the canvas (wrapper div) with pixels (pixel divs). The canvas does not populate correctly. Here is the link to m ...

Vue.js dynamic HTML elements are constantly changing and evolving

Is it possible to dynamically add elements to the content? See example below: <template> {{{ message | hashTags }}} </template> <script> export default { ... filters: { hashTags: function(value) { ...

Developing components with jQuery

I have a JavaScript program that works perfectly when I use the following line of code: li = $("<li data-role='collapsible' data-iconpos='right' data-inset='false'></li>"); However, if I change the above line ...

Tips for showcasing personalized validation alerts with jQuery in the HTML5 format?

One of the Javascript functions I designed is for validating file extensions before uploading: function validateFileExtension(field, extensions){ file_extension = field.val().split('.').pop().toLowerCase(); if ($.inArray(file_extension,exten ...

What is the best way to call a function within another function only when a certain div is not clicked?

Throughout my code, I am utilizing the campusInfo(id) function. This particular function, in turn, calls another function named campusInfo_2(HouseID). One scenario where I am invoking campusInfo(id) is when a specific div is clicked. In this instance, I ...

What is the best way to display a Markdown field in Vue3?

After following the Nova documentation, I successfully linked a Markdown field to a TEXT database type: Markdown::make('Content', 'content_text') Now, I am trying to display it correctly on my web application: https://i.sstatic.net/E8 ...

What is the most effective way to access content from a webpage that is rendered

Is there a reliable way to download from links on a JavaScript rendered webpage using Python as the preferred language? I have attempted to use the Selenium Python bindings on a headless server, but it has proven to be slow, error-prone, and unable to acc ...

"Troubleshooting issues with data loading using React's useEffect function

While working on my project, I encountered a strange issue where the isLoading state is being set to false before the data fetching process is completed. I am using the useEffect hook to show a loading spinner while fetching data from an API, and then disp ...

What are some ways to detect TypeScript type errors in the data of a Vue component?

Recently, I delved into Typescript development using Nuxt-ts and Vue 2. My goal was to steer clear of class-style components so I opted for the following approach. I created my Interfaces in a folder named /types. Whenever I needed to declare a type in a ...

Transmit a JSON object while simultaneously receiving the corresponding response

As part of a school project, I need to work with JSON. The server will process the file and send back a response in JSON format. This entire exchange needs to be done using JavaScript. I recently learned about the XMLHttpRequest object, but I'm still ...

Transmitting text data within Google Analytics

I'm currently working on binding events for tracking with Google Analytics. When calling GA, we also have the option to send a value along with it. My goal is to send a value using a DOM selector. For example, when I use: myValue=function(){return ...

Upcoming construction: Issue encountered - The Babel loader in Next.js is unable to process .mjs or .cjs configuration files

Within my package.json file, I have set "type": "module" and "next": "^12.2.5". In my tsconfig.json: { "compilerOptions": { "target": "ES2022", "module": "esnext ...

Distinguishing between creating controllers in AngularJS

I am a beginner in the world of AngularJS and I have come across two different examples when it comes to creating a controller. However, the one that is more commonly used doesn't seem to be working for me. The problem with the first example is that ...

PHP regular expression /only match 10 whole digits/;

Currently, I am working on updating a PHP script that contains the following code snippet: function CheckNumber(MyNumber) { var MN = /^\d{10}$/; if (MN.test(MyNumber)) { return true; } return false; } The current script enfor ...

Error 405 (Invalid Method) in VueJs: You are not allowed to use

I have a route defined in the group with 'prefix'=>'admin' in web.php Route::post('/slideUpdate/{id}','SlideController@postSlideUpdate') In Add.vue, I am calling the update() function in methods axios.patch(`/a ...