The ultimate guide to passing slots from Parent to Child Components seamlessly using PrimeVue

As I venture into creating my own component library by wrapping some PrimeVue components, I am faced with the challenge of handling numerous slots.

Many PrimeVue components come with a large number of slots, and passing each one individually seems like a daunting task.

Luckily, I stumbled upon this ingenious solution that allows for passing all slots from Parent to Child Component seamlessly.

For instance, consider the following implementation using the PrimeVue Dropdown component:

<Dropdown v-bind="$attrs" >
  <template v-for="(_, name) in $slots" #[name]="scope">
    <slot :name v-bind="scope" />
  </template>
</Dropdown>

However, an issue arises when working with TypeScript as it raises an error on #[name] due to the absence of an index signature on type 'DropdownSlots':

Element implicitly has an 'any' type because expression of type 'string | number' can't be used to index type 'DropdownSlots'.

No index signature with a parameter of type 'string' was found on type 'DropdownSlots'. ts(7053)

Are there any suggestions for a workaround to this problem?

Answer №1

Just because there is a TypeScript error, it doesn't automatically mean that the code is incorrect. The Vue templates lack type safety by default and need external tools (such as vue-tsc, IDEs) for type checking, which can sometimes come with their own issues.

Instead of using types in a template, it is often simpler to manipulate children and props within the render function. This approach also helps avoid challenges related to types in templates. For a basic wrapper component, the setup may look like this:

setup(props, { slots }) {
  return () => h(Dropdown, props, slots);
}

Answer №2

There is a fantastic solution I discovered called Vue Forward Slots that addresses all issues related to forwarding slots: https://www.npmjs.com/package/vue-forward-slots

This is how I implemented it to forward all slots, including the default slot:

  <ForwardSlots :exclude="[]">
    <Dropdown v-bind="$attrs" />
  </ForwardSlots>

I look forward to seeing something similar integrated into core vue.js in the future.

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

Tips for setting cookies while making a post request in Vue.js

I am working on sending post data to a django Restful API using vuejs. Here is the code snippet I have been working with: <script> import axios from 'axios' import VueCookies from 'vue-cookies' VueCookies.set("csrftoken", "00000 ...

What is causing ESLint to point out the issue with the @inheritdoc tag?

My code in ESLint is throwing an error about a missing JSDoc return declaration, even though I have included an @inheritdoc tag there: https://i.sstatic.net/QGxQh.png Here is the section from the interface I am inheriting from: export interface L2BlockSo ...

Changing TypeScript Enum from String to Number in Angular

Does anyone know how to convert a Typescript enum value to a number for display in a dropdown but passing the numeric value on submit? Here is how the enum is currently set up: I am currently able to output the string key of the object when it is emitted ...

One effective method for populating my form following the retrieval of JSON data

Currently, I am working on a VueJs project where I am fetching data from a Mysql database using Axios. My next step is to implement an edit option for user profiles. I am wondering what the most efficient method is for populating the form with the fetche ...

Example of a floating undo bar using a dynamic function in a Vuex store module

Issue Overview Trigger the mutation or action of Vuex store module A to execute an external function. This external function can belong to another Vuex store module (e.g. B). A should store a reference to the external method (e.g. mutation or action from ...

Issue with Angular standalone component importation causing rendering issue in HTML

Recently, I started working with Angular and I am currently creating a clone using Firebase. While working on this project, Angular is throwing two errors at me: The Component AppComponent is standalone and cannot be declared in an NgModule. Should it b ...

Vue components: Using `object[key]` as a prop does not trigger reactivity

I'm attempting to bind a value from an Object into the prop of a Component, but unfortunately it is not reacting as expected. Despite using $this.set, the reactivity issue persists. Below is my template: <div class="grid"> <emoji-card v-fo ...

"Enhance your coding experience in VS Code with intelligent auto-completion for JavaScript files using type definitions that support import

Hey there! I've been experimenting with declaring custom types in d.ts files and using them in jsdoc annotations in JavaScript files to enable intellisense in VS Code. Let's take a look at an example: In the file types.d.ts import { Request } ...

No output was generated by Typescript for the file located at '/path/to/file.ts'

My typescript library transpiles smoothly using tsc with the provided configuration: { "compilerOptions": { "target": "es6", "module": "commonjs", "lib": [ "es6", "es5", "dom", "es2017" ], "declaration": true, ...

The second guard in Angular 5 (also known as Angular 2+) does not pause to allow the first guard to complete an HTTP request

In my application, I have implemented two guards - AuthGuard for logged in users and AdminGuard for admins. The issue arises when trying to access a route that requires both guards. The problem is that the AdminGuard does not wait for the AuthGuard to fini ...

Implementing Vue Js checkboxes using dynamic JSON data

I have a product search page where users can filter based on selected category. When a category is chosen, the API returns the following response: "customFields":{ "select":{ "brand":{"options":[ "nike", "adidas","puma"]}, "colour" ...

Utilizing Typescript to implement an interface's properties

After declaring an interface as shown below interface Base { required: string; } I proceeded to implement the interface in a class like this class MyClass implements Base{ method(): void { console.log(this.required); } } However, I ...

Discover the power of using SVG sprites in Vue JS for dynamic, visually appealing CSS background

Utilizing an SVG sprite file in a Vue JS HTML template is successful with the following setup: <svg class="icon"> <use xlink:href="~@/assets/images/icons.svg#edit"></use> </svg> The contents of the icons.svg file typically resem ...

Using Vue JS to handle image upload with "PROP MUTATING"

Apologies for any language barriers or inaccuracies in my English. I have a single component designed specifically for image uploads. It is currently being utilized in two forms: an add form and an edit form. In the edit modal, the Image URL is passed as ...

Generate sequential keys for objects in an array

Looking for assistance; Imagine I have an array of objects structured like so { 'Name:'ABC', 'Code': 'BP' } What is the most effective method to add an incremental attribute to this array in typescript. [{&apo ...

What is the best way to eliminate mouse click release events?

Encountering an issue with my Vue/Vuetify Dialog that closes when clicking outside of it as expected. The problem arises when there is a text field inside the dialog. If I accidentally select the content and release the mouse outside of the dialog, it als ...

Is there a navigation feature in VueJS that functions similarly to React Router?

I am currently working on enhancing the navigation experience of an existing vueJS application that utilizes Vue Router. When working with React, I typically structure breadcrumbs in the following manner: <Breadcrumbs> <Route path="/users&q ...

In VueJS, the variable 't' has been given a value but is never actually utilized in the code

I'm encountering errors while importing a template in vue.js. Here are some of the errors that I'm facing: 't' is assigned a value but never used no-unused-vars 1068:33 error 'calendarEvents' is not defined ...

Display a JSON file within an Angular application using HTML

I have a JSON (link to the JSON) that I want to display in an html file. JSON: { "recipes": { "FRYING": { "Anchovies": [ [ {"500": "thawing"} ], [ {"60": "nothing"} ] ], "Codfis ...

What is the correct key to use when using V-for with basic arrays?

Can anyone provide insight on how v-for is used with basic arrays? Here are some possible options that I've identified. The first option seems to suggest that basic arrays are not suitable for v-for loops. Should arrays of objects be used instead, wi ...