What is the best way to create Vue components with identical names but different functionality?

I created a list of divs using v-for. When a user clicks on one of the divs, a modal window opens to display more details about that specific div.

<PlayerModal :player="selectedPlayer" />

<div v-for="player in players" ...>
    <div @click="openModal(player)">
        {{ player.name }}
    </div>
    ...
</div>

The modal makes a get request to retrieve additional information and saves it so that when the modal is closed and opened again, the cached information is displayed. I tried using a keep-alive tag for this purpose, but I'm unsure how to handle dynamic rendering of information from different divs into a single modal component.

One approach I considered was to have one modal-component per div, each with its own keep-alive tag. However, since all modal components would have the same name, I'm not sure how to toggle them on and off individually.

<div v-for="player in players">
    <keep-alive>
        <PlayerModal :player="player" :is=" ??? " />
    </keep-alive>
</div>

Any advice or assistance on this matter would be greatly appreciated! Thank you!

Answer №1

To make the dynamic component work, simply include a key.

<div v-for="player in players">
   <div @click="openModal(player)">
       {{ player.name }}
   </div>
</div>
<keep-alive max="10">
    <component :player="selectedPlayer" :is="PlayerModal" :key="selectedPlayer.id"/>
</keep-alive>

To see cached components, check this.$children and the child component's _inactive status.

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

What is the best way to initially conceal content and then reveal it only after an ajax call is made?

Currently, I have a situation where content is revealed after the callback function of a .js library (typed.js) executes. Here is the script I am using: Javascript $(function(){ $("#logo-black").typed({ strings: ["Nothing^450&Co^250.^500" ...

Adding a Vue component to HTML using a script tag: A step-by-step guide

Scenario: I am working on creating a community platform where users can share comments. Whenever a comment contains a URL, I want to turn it into a clickable component. Challenge Statement: I have a dataset in the form of a string and my aim is to replac ...

Issue encountered in IE9 and IE10 when loading the angular library results in the "SCRIPT5007: Object expected" error for Angular JS

I am currently working on an AngularJS application that needs to be compatible with Firefox, IE 9, and IE 10. We are using the latest version of the AngularJS library (currently at 1.3.15). Our serverside is based on Java in the JavaEE platform, running on ...

Error: The VueComponent.onResize function encountered an uncaught TypeError because it cannot read the property 'offsetHeight' of an undefined variable

<div ref="controls" class="varie controls"> <div class="control" v-if="spin!='"> <span>Saved datee</span><span>{{ }}</span> </div> <button v-on:click="apply()">Aopply</button> </div& ...

What is the secret behind the functionality of this code?

Could you clarify the following Data Viewer with Laravel 5.3 and Vue.js for me? In this tutorial, a trait called DataViewer is created which has the following code: trait DataViewer { // $query should be a Query Builder public function scopePagi ...

"Utilize Meteor to transmit emails to internal email addresses within the intran

I have successfully developed a Meteor App to manage requests. However, I am facing an issue where I need emails with default text to be sent whenever a request is created or updated. The challenge lies in the fact that I am operating within an intranet e ...

Display the URL with proper formatting in the print function

I am trying to create a table with clickable URLs in a "Link" column, but the URLs are too long and I want to show a custom title instead. So far, I have used the following code: str = "Test Title"; link = str.link("https://my_long_url.com/v1.0/ui/index. ...

JavaScript error message stating that the setAttribute function is null

As a newcomer to JS, I am currently working on creating a task list webpage that allows users to submit projects and create task lists within each project with designated due dates. In order to generate unique ID tags for projects and tasks, I utilized UU ...

Utilize jQuery method in HTML to perform parsing operation

Is it possible to invoke my jQuery function from within my HTML code? Here is the code snippet: HTML section: <td style="text-align:left;" align="left" > <div id="bulletin-timestamp" > doTimeStamp(${bulletinTimeStamp[status.index ...

Discover the secret to smoothly scrolling to an element in ReactJs

Currently, I am in the process of constructing a Single Page Application (SPA) using React and one key functionality I need to implement is navigation that scrolls to specific sections on the page when clicked. This behavior is similar to how anchor tags w ...

Encountering a discord bot malfunction while on my Ubuntu server

My discord bot runs smoothly on my Windows 10 setup, but when deployed to the server running Ubuntu Server 20, it encounters a specific error. The issue arises when trying to handle incoming chat messages from the server. While I can read and respond to m ...

Unable to access a frame inside an iframe and frameset using JavaScript when both domains are identical

I am attempting to use JavaScript to access an HTML element within a nested frame in an iframe and frameset. The structure of the HTML is as follows: <iframe id="central_iframe" name="central_iframe" (...)> <frameset cols="185, *" border="0" ...

What is the best way to create two MUI accordions stacked on top of each other to each occupy 50% of the parent container, with only their contents scrolling

I am looking to create a layout with two React MUI Accordions stacked vertically in a div. Each accordion should expand independently, taking up the available space while leaving the other's label untouched. When both are expanded, they should collect ...

"Update content dynamically with PHP-AJAX only when there are

Hey there, I've got a comment system that's being constantly updated with jQuery. My goal is to only refresh it when new records are added to the database. Is this possible? Please forgive any mistakes in my English, as I'm using Google Tra ...

Encountering an issue with Material-UI and Next.js: "TypeError: theme.spacing is not a function

Encountering an issue after modifying _app.js to dynamically generate a material UI theme. I've been following the implementation example provided by the material-ui team at: https://github.com/mui-org/material-ui/tree/master/examples/nextjs. To summ ...

loading JSON file using dynamic JavaScript techniques

My Raspberry Pi is running a C++ application in the background that performs complex mathematical calculations based on sensor input and generates results every second. I want to showcase this data on a website by having the application create a JSON file ...

Simple CSS for creating a "red alert badge" with a number count

Looking for the best way to display the popular red notification indicator with count in a consistent manner across various browsers. It seems tricky to achieve a design that looks seamless on all platforms, as different browsers interpret paddings differe ...

Combining two arrays in React using TypeScript and showcasing them in a single list display

I am working on a React TypeScript project and have two comma-separated strings that I am converting into arrays. One array contains the file names, and the other contains the file link paths. My goal is to merge the two arrays so that the first item in th ...

Ways to retrieve an isolated scope in a directive without utilizing a template

It is clear that when I assign a controller to an element, the element along with its children can access the controller's scope. However, it is surprising that when I include a directive with an isolated scope on an element (as an attribute), I woul ...

Guide to incorporating PHP code into JavaScript

Encountering an issue when trying to combine PHP syntax within JavaScript syntax. var id = $("#data-1").val(); var url = '<?= base_url('home/alone/'); ?>'id''; console.log(url); I am trying to append the id at ...