How can I manually trigger the rendering of a dynamic component in Vue 3?

Is there a way to ensure that all dynamic components are rendered at once?

Currently, only the component of the last tab is getting mounted. I want to mount them all at the same time.

const addTab = (symbol: string) => {
  const id = nextTabId++;
  tabs.value.push({
    id,
    symbol,
    component: markRaw(ChartComponent),
  });
  activeTabId.value = id;
};

onMounted(() => {
  addTab("USD");
  addTab("EUR");
});

  <KeepAlive>
    <component
      :key="activeTab?.id"
      :is="activeTab?.component"
    />
  </KeepAlive>

Answer №1

Avoiding KeepAlive and opting for v-show

<div v-for="tab in tabs" :key="tab.id">
  <component :is="tab.component" v-show="activeTabId === tab.id"/>
</div>

Answer №2

When dealing with tabs, only one content component is necessary at a time. I have implemented a straightforward solution which I hope will be helpful.

<script setup lang="ts">
import { onMounted, ref, reactive } from 'vue';
import ChartComponent from './ChartComponent.vue';
interface Tab {id: number, symbol: string}
const tabs: Tab[] = reactive([]);
let nextTabId = 0
const activeTabId = ref(0)

const addTab = (symbol: string) => {
  const id = nextTabId++;
  tabs.push({
    id,
    symbol,
  });
};

onMounted(() => {
  addTab("USD");
  addTab("EUR");
});
</script>
<template>
<div class="tabs" v-if="tabs.length">
  <button v-for="tab in tabs" :key="tab.id"
    @click="activeTabId = tab.id"
  >
    {{tab.id}} - {{ tab.symbol }}
  </button>
</div>
<div class="tab-content">
  <component :is="ChartComponent" :symbol="tabs[activeTabId].symbol"/>
</div>
</template>

Note: If you require different components for different tab items, they can be placed into the tabs array with a new component property without issue. Dynamic props can be passed using the v-bind attribute in the component.

Demo

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

Issue with Signature Pad feature "Type It" function not functioning as expected

I am currently experimenting with this GitHub repository. The "Draw It" feature is functioning as expected, however the "Type It" functionality does not populate any value in my hidden input field. Please review the VF page code and screenshot below for f ...

Passing props to children in the Next JS Layout component is a crucial aspect of

I recently came across a code snippet that effectively resolved my re-rendering issue in Next JS when switching pages. However, I am now faced with the challenge of sending props to the children component. In my layout.js file, I have managed to send props ...

Validating Firebase data for null values

Hey there, I'm currently working on a simple coding project but seems to be encountering some roadblocks. The main objective of the code is to determine if a username exists in the system or not. Here's a snippet of the data structure and codes ...

Display hyperlink depending on spinner value

I stumbled upon this JavaScript spinner that displays a countdown feature, which I find quite interesting. The spinner counts down from 15 seconds. I'm curious if it's feasible to customize it so that if you land on a specific category like geogr ...

Incorporating and utilizing a variable from a separate page in React Native

Screen 1 export class Register extends Component { constructor(props) { super(props); this.state = { number1=0 }; render() { const { number1 } = this.state; v ...

Tips for adjusting column width with flexbox intersections

I am currently working on a React web application that has minimal styling. I have divided the content into 3 columns, with the leftWrap being col-3, rightWrap being col-4, and the remaining width is for centerWrap. I want to apply flex ...

Sending an object over to a different page

Need some assistance with displaying JSON data that is being repeated using ng-repeat. { "title": "image title", "description": "Lorem ipsum dolor sit amet, per ea ferri platonem voluptaria, ea eum ubique ornatus interpretaris. Dolore erroribus reprimique ...

Disable the outer div scrolling in VueJS, but re-enable it once the inner div has reached the bottom of

I am currently working on a webpage that contains multiple divs stacked vertically. Here is the concept I am working with: Once the scrollbar reaches the bottom of the first div, the outer scrollbar will be disabled and the inner scrollbar will be enabled ...

Is THREE.js disregarding the line thickness when using a BufferGeometry with LineBasicMaterial in a THREE.Line object?

Here is a minimal test case I've put together that showcases my issue: http://pastebin.com/TPXFgxLj The desired outcome should be a set of red lines with a line width of 19. However, the current result shows a set of red lines with what appears to be ...

Tips for creating animations using parent and child components in Angular

Despite my best efforts, it seems like this should be functioning properly... but unfortunately it's not... I'm attempting to achieve a transition effect on the parent element (ui-switch-groove) while the child element (ui-switch-dongle) moves. ...

Caution in Three.JS: Potential WebGL Issue with Texture Mapping in Material Creation

I'm facing a challenge with a JSON file (map.js) that I use to load my geometry and material settings. This file is quite large, making manual edits challenging. Here is a snippet of what it looks like: "materials": [ { "DbgColor" : 2632490, "DbgInd ...

Tips for creating a cursor follower that mirrors the position and size of another div when hovering over it

I am trying to create a cursor element that follows the mouse X and Y position. When this cursor hovers over a text element in the menu, I want it to change in size and position. The size of the cursor should match the width and height of the text being h ...

Is it detrimental to have an excessive amount of simultaneous AJAX connections?

In the process of developing a JavaScript based application of significant size, I frequently encounter situations where up to eight (8) AJAX requests are initiated concurrently. In older browsers such as IE6, this can result in certain requests being term ...

How to leverage async/await within loops in Node.js for optimized performance and efficiency

Hey there, I'm working on my nodejs api where I need to fetch data inside a loop and then perform another loop to save data in a different table. Can anyone guide me on how to achieve this? Below is a snippet of what I have attempted so far without su ...

Having trouble with NextJS not updating state upon button click?

I am encountering a problem with my NextJS application. I am attempting to show a loading spinner on a button when it is used for user login. I have tried setting the `loading` state to true before calling the login function and then reverting it to fals ...

Encountered a parsing error when attempting to integrate SCSS with webpack and babel setup

I am facing an issue while trying to integrate SCSS into my webpack and babel setup. When running npm run build, I encounter an error that I'm unfamiliar with. As a beginner in using webpack and babel, I'm unsure about the necessary changes requ ...

Retrieving a JSON object using a for loop

I'm working on a basic link redirector project. Currently, I have set up an Express server in the following way: const express = require('express'); const app = express() const path = require('path'); const json = require('a ...

Storing JSON values dynamically

On a PHP webpage, I am sending values using JSON. $result = mysql_query("SELECT * FROM user_info"); $i=1; while($row = mysql_fetch_array($result, MYSQL_NUM)) { if(is_array($row) && count($row)>0) { $res[&a ...

Retrieving data from Ajax and passing it to PHP

I am currently facing an issue with my Ajax code. When I try to access a textfield variable in PHP after alerting the value successfully, I receive an error stating Undefined index. Javascript Code <SCRIPT> function sendData(UserID) { var name = en ...

Sequential jQuery AJAX requests triggering in an incorrect sequence

One challenge I am facing involves a select list with bundles of short texts. Upon selecting a specific bundle, the texts are displayed in two tables. Each table row has a "Delete" icon to remove a text from the bundle. I am aiming to refresh both the tabl ...