Using Vue to display components within a collapsible accordion component

I'm currently working on a project involving multiple accordion expandable drop-downs. Here is a rough example of what I am dealing with: https://i.sstatic.net/igvrU.png

Each step within the accordion should load a different component when expanded. For instance, "Step 1" would trigger the display of the StepOne.vue component.

To handle the Accordion functionality, I have developed a separate component:

<template>
<div id="accordion">
  <div class="card">
    <div class="card-header" :id="givenId">
      <h5 class="mb-0">
        <button class="btn btn-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
          {{stepStr}} // step 1,2,3 -etc
        </button>
      </h5>
    </div>

    <div id="collapseOne" class="collapse show" :aria-labelledby="givenId" data-parent="#accordion">
      <div class="card-body">
      // A dynamic component needs to be loaded here.
      </div>
    </div>
  </div>
</div>
</template>

My main challenge lies in finding a way for each accordion step to load a unique component without duplicating the entire HTML code for each step.

What I envision is something similar to this structure:

Display.vue

<template>
  // Step 1
  <accordion :id="step-1" :stepStr="'Step 1'">
      <Step-One></Step-One>
  </accordion>

  // Step 2
  <accordion :id="step-2" :stepStr="'Step 2'">
      <Step-Two></Step-Two>
  </accordion>

  // Step 3
  <accordion :id="step-3" :stepStr="'Step 3'">
      <Step-Three></Step-Three>
  </accordion>
</template>

However, implementing this approach has proven challenging. I have also explored passing the component as a prop to be rendered within the accordion, but this does not seem to provide a viable solution either.

Can anyone suggest a method for passing a component to the child Accordion component so that it can correctly render each "step" component?

Answer №1

Make sure to utilize the slot feature in your accordion component to display the code inside the accordion tags.

<slot></slot> 

Within your accordion component, insert the following code snippet where you want to load a dynamic component:

<div id="collapseOne" class="collapse show" :aria-labelledby="givenId" data-parent="#accordion">
      <div class="card-body">
          <slot></slot> // I want to load a dynamic component here.
      </div>
    </div>

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

Develop a constructor that can be injected

Delving into the world of AngularJS as a beginner, I am starting to grasp the intricacies and distinctions between factory, service, and controller. From my understanding, a factory serves the purpose of returning a "value object" that can be injected. Mos ...

Error: Unable to access attributes of null object (specifically 'disable click')

Currently, I am integrating react-leaflet with nextjs. Within the markers, there are popups containing buttons that open another page upon click. However, I have encountered an error when navigating to the new page: Unhandled Runtime Error TypeError: Canno ...

Exploring Molecular Structures with ThreeJS - Understanding the Mechanics of Double Bonds

Currently working with threejs and experimenting with the example found in this link: The challenge I'm facing is related to creating double bonds between grey and red spheres in the example. While I came across some resources suggesting ways to achi ...

Fade out all the other images using jQuery

I have created a jQuery code to fade images, but when I move the mouse over them all images fade simultaneously! $(".playThumb").fadeTo("normal", 1); $(".playThumb").hover(function() { $(".playThumb").each(function() { if ( $(this) != $(this) ...

How can I add .htaccess to Vue production when using npm run build?

While using Vue CLI 3 and vue-router with history mode, I encountered this particular issue. Upon further investigation, I discovered that inserting a .htaccess file inside the dist folder after running npm run build resolved the issue. Is there a way to ...

Moving the Promise.all feature from AngularJs to VueJs

I'm currently facing a challenge with moving a function from AngularJs to VueJs. I would really appreciate any help or suggestions you may have! items = { one: {...details here...}, two: {}, } In AngularJs: var promises = []; var deferred = $ ...

React Native Navigator screen displaying unexpectedly

I am currently utilizing React Navigation within my react native application and following its authentication flow. The app.js file will determine whether to render the authentication screen or home screen based on the user's authentication state, wh ...

Error Alert: React Native object cannot be used as a React child within JSON, leading to an Invariant Violation

When using React-Native: To start, here is the example code of a json file: Any placeholders marked with "..." are for string values that are not relevant to the question. [ { "id": "question1" "label": "..." "option": [ { "order": 1, "name": "..."}, ...

Initial input firing empty string on React's onChange event

Issue - When I input text for the first time, no string output is displayed. As a result, the last character of the input is not showing up. What could be causing this? Specific focus - Concentrating on the name property const [options, setOptions] = useS ...

Update the page by selecting the refresh option from the drop-down menu

In my script, I have different views showing information retrieved from a database. One view displays the quantity of a product sold each month, another shows sales, the third presents profits, and the last exhibits calculated percentages using sales and p ...

Passing the value selected from a datepicker to PHP without refreshing the page through the use of XMLHttpRequest: Guide

Is it possible to transfer the datepicker value to PHP without reloading the HTML page by using XMLHttpRequest()? The intention is to utilize this date value for a subsequent query and present it on the same HTML page. <input type="date" id="month" o ...

vue-form and vue-material are not compatible with each other

In my experience, using a Vue form on a regular HTML <input> element allows validation to work as expected. However, when I switch to using the <md-input> element, the validation no longer functions and an error message is displayed: Element ...

Switching between different sections of a webpage

Seeking assistance with implementing a transition effect between sections on a single-page application. All sections are located on the same page, but only one section is displayed at a time. When an event occurs, the display property of the requested sect ...

Optimize your website by caching static pages and content using Node.js

When it comes to caching static content using nodejs, there seem to be two main methods that can be utilized: The first method involves using nodejs and utilizing the following code: app.use(express.static(path.join(__dirname, 'public'), { max ...

Is there a way to display both the label and the input field in a single line?

How do I align the label and input on the same line with a spacing of 1.5cm between them? I tried using this resource, where it mentioned using form-inline but wasn't successful in achieving the desired layout. https://i.sstatic.net/7MW2H.png Appre ...

Exploring the intricacies of JSON object retrieval

I'm currently working on a form that allows users to submit address details for a selected location. However, before submitting the form, I want to give the user the ability to preview the address that will be sent. The addresses are stored within a J ...

The splice function in Angular is mistakenly removing a different record instead of the intended one that was

An issue with the Angular splice function is causing it to delete the wrong record. Here is the code snippet in question. Within the code below, when attempting to delete the record labeled "Pat," the code ends up deleting the record labeled "Max." Please ...

basic computation of whole and decimal values using jquery

I'm having trouble multiplying 2 values in my code where the quantity is an integer and credit price is a decimal number. However, when I run the script, nothing seems to happen. Can someone please help me identify and resolve this issue? Any insight ...

What is the process for updating the dropdown list value based on the radio button that has been selected?

Is there a way to retrieve the value of a radio button and utilize it in the where clause? <input type="radio" name="radiobtn" value = "Yes" onclick="GetLockIn();" >Yes <input type="radio" name="radiobtn" value = "No" onclick="Ge ...

Strange anomaly discovered in Backbone Forms

ISSUE: I developed a custom backbone editor, but when trying to initialize it with Backbone Forms, I encountered an issue where it couldn't find the schema for my editor. Upon investigation, I discovered that Backbone was looking for the schema in th ...