What is the best way to send data to a component through a slot?

Currently, I am working on a Vue application that utilizes pimcore and twig in the backend. My task involves creating a component that can receive another component (slot) and dynamically render it with props. Here is the root structure found in viani.twig.html:

<div>
    <viani-accordion>
        <viani-filter-checkbox v-slot="{filterRows}"></viani-filter-checkbox>
    </viani-accordion>
</div>

This snippet shows the basic setup where viani-accordion serves as the parent component while viani-filter-checkbox acts as a slot that needs to be rendered with specific props.

The VianiAccordion.vue file contains the following:

<template>
    <div class="wrapper">
        <AccordionTitle v-for="(item, index) in dataToRender" :item="item" :key="index">
            <slot :filter-rows="item.items"></slot>
        </AccordionTitle>
    </div>
</template>
<script>
    import AccordionTitle from './Accordion-Title';
    export default {
        name: "Viani-Accordion",
        components: {AccordionTitle},
        data() {
            return {
                dataToRender: [
                    // Data items here
                ]
            }
        },
    }
</script>

Furthermore, there is a child component called Accordion-Title.vue, which renders the slot passed down through multiple components:

<template>
    <div v-if="isOpen" class="child-wrapper">
        <slot :filterRows="item.items"></slot>
    </div>
</template>
<script>
    export default {
        name: "Accordion-Title",
        props: {
            item: {
                type: Object,
                default: null
            }
        }
    }
</script>

Finally, we have the Viani-FiltersCheckbox.vue component:

<template>
    <div>
        <FilterCheckboxRow v-for="(item, index) in filterRows" :item="item" :key="index"/>
    </div>
</template>

<script>
    import FilterCheckboxRow from './FilterCheckboxRow'
    export default {
        name: "VianiFilterCheckbox",
        components: {
            FilterCheckboxRow
        },
        props: {
            filterRows: {
                type: Array,
                default: function () {
                    return []
                }
            },
        },
    }
</script>

My main challenge lies in passing the necessary props (filterRows) to the Viani-FiltersCheckbox.vue component, which is being rendered as a slot. Despite referring to various resources such as this and this, I still struggle to identify the mistake and successfully retrieve the required props.

Answer №1

It appears that you are attempting to access your props using props.XXX. This practice is typically reserved for templates in functional components. In other cases, props should be accessed without the props. prefix (e.g., props.item.items should simply be item.items).

To pass the filterRows from the scoped data to the child component, define a <template> and then place your child within it, binding the filterRows there:

<viani-accordion>
    <!-- ORIGINAL CODE: -->
    <!-- <viani-filter-checkbox v-slot="{filterRows}"></viani-filter-checkbox> -->

    <template v-slot="{filterRows}">
        <viani-filter-checkbox :filterRows="filterRows"></viani-filter-checkbox>
    </template>
</viani-accordion>

https://codesandbox.io/s/musing-merkle-c4diq?fontsize=14&hidenavigation=1&module=%2Fsrc%2FApp.vue&theme=dark

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

The error message you are encountering is: "Error: Unable to find function axios

Can't figure out why I'm encountering this error message: TypeError: axios.get is not functioning properly 4 | 5 | export const getTotalPayout = async (userId: string) => { > 6 | const response = await axios.get(`${endpoint}ge ...

Creating a toggle label using just CSS: a step-by-step guide

Recently, I was trying to create a toggle label using HTML, CSS, and JavaScript. Here is the code snippet that I experimented with: function genre_itemclick(item){ if(item.classList.contains('light_blue_border_button')) { ...

The custom stylesheet added to index.html is not taking precedence over the Bootstrap 5 styles applied through NPM

In my Vue 2 project, I encountered an issue with Bootstrap 5.3 when installed via NPM. My custom CSS stylesheet in the index.html file was not able to override Bootstrap's CSS. Interestingly, I faced no problems when using Bootstrap from a CDN. Addit ...

Generate a binary string using JavaScript and then transform it into C#

I have an upload section in my JavaScript program. I utilize JS FileReader to obtain a binary string of the uploaded document before sending it to my C# WebApi for storage on the server. JavaScript Code let myFile = ev.target.files[0]; if(myFile.size > ...

AngularJS: incorporating various functionalities within a single controller

I have a basic AngularJS controller that I am working on, and I would like it to include two separate functions: var app = angular.module('searchApp', []); app.controller('searchCtrl', function($scope, $http, $log) { //Function 1 ...

Utilizing React Material-Table to Trigger a React Component through Table Actions

I have set up a datatable using the code below. <MaterialTable icons={tableIcons} title={title} columns={columns} data={data} actions={[ { icon: () => <Edit />, t ...

Determine in jQuery whether a Value is present within a JSON dataset

[ { "link" : "images/examples/image-3.png", "image" : "images/examples/image-3.png", "title" : "copy" }, { "link" : "images/examples/image-3.png", "video" : "video placeholder", "title" : "c ...

Enhancing NodeJS performance when manipulating arrays

I'm seeking a way to retrieve a user's chat history with other users from a separate collection in NodeJS and MongoDB. I have concerns about the potential performance impact of running the code below due to the nature of NodeJS. While I could d ...

Why am I unable to access the most up-to-date release of postcss?

I am currently utilizing vue-cli which relies on the presence of postcss. Upon executing npm audit, I am alerted about vulnerabilities within postcss and advised to upgrade to a newer version. How can I accomplish this task? I attempted using npm update, ...

Discover how to use Jest and Enzyme to test for any string as the defaultValue on an input field

As a beginner in the world of testing, I've been exploring the airbnb / jest documentation to troubleshoot an issue with a test. So far, I haven't been able to come up with a solution that actually works. The snapshot contains a defaultValue tha ...

Is it feasible to utilize express.static twice in Express.js 4.x?

I am seeking to create a unique 404 page that includes HTML, CSS, images, and icons. Upon reviewing my project structure, I have observed that my 404 folder functions correctly when replacing the initial public static homepage. However, I suspect that I ma ...

Utilize React Helmet for Dynamic Page Titles

Currently, I am utilizing Gatsby with react-helmet to manage the title and meta tags in the head of my website. However, I am eager to find a way to also display this title in the actual text of the page through a global <Header /> component. In proj ...

The Kendo Grid is refusing to show up within the popup window

I am new to using Angular 2 and Kendo UI. Currently, I am attempting to include a grid inside my pop-up window. While I have successfully displayed the pop-up, adding the grid has proven challenging. The grid is not appearing as expected ...

Multiple invocations of Vue.js function

When running the code and checking the browser console, you may notice that the function "formatName()" is being called multiple times. But why does this happen if the race data isn't being updated? Well, changing the function "amIStarted()" to "retur ...

What is the best way to insert a variable URL in JavaScript code?

When working with PHP, I often create a declaration similar to the following (taken from an example on Stack Overflow): <script type="text/javascript"> var templateurl = "<?php bloginfo('template_url') ?>"; </script> Subse ...

Discover the steps for incorporating the substring method into JavaScript to manipulate input strings

Is there a way to extract the specified portion of text from an input string using HTML and Javascript? <!DOCTYPE html> <html> <body> <input type="text" value="exampleString"></input> <button onclick=& ...

When using ng-repeat with Angular ui-bootstrap and tabs, the new tab will not be selected if there are no existing tabs present

To showcase the problem I'm facing, please refer to this link: http://codepen.io/pietrofxq/pen/ZLLJdr?editors=1010 Click on "remove tabs" and then on "add tab" The challenge at hand involves using a loop with ng-repeat to display tabs. At times, the ...

Is the Spring MVC ModelAndView object being returned?

After clicking a link on a jsp page, I have a GET method that is instantiated as: HTML: <div class="panel accordion clearfix" id="dispdir"> <script type="text/javascript"> window.onload = function() { //showDirectorySe ...

Some elements in the DOM appear to not be rendering even though they are present in the HTML source code

As a newcomer to web development, I have stumbled upon a perplexing issue. In my usage of d3.js (specifically the script found here), I have been attempting to incorporate additional features. I modified my JavaScript code to include a simple <p> ele ...

Use jQuery to permit only numeric characters and the symbol "-" to be entered into a textbox

I have been working on a JQuery script that only allows users to input numbers in a text field. However, I am now trying to modify the code to also allow users to enter "-" (hyphen), but so far it's not working as expected. If anyone can provide assis ...