What's the Catch with Vue's Named Slots?

When working with Named Slots in Vue (using the older, more verbose API for component slots), if there is a reusable component with a template structured like this:

<template>
 <div v-for="field in formFields">
  <slot name="`${field.key}_PREPEND`">
   <span hidden></span>
  </slot>
  <slot name="`${field.key}_FIELD`">
    <slot name="`${field.key}_LABEL`">{{ field.label }}</slot>
    <slot name="`${field.key}_CONTROL`">
      <input v-if="field.type === 'text'" v-model="model[field.key]"></input>
      <input type="checkbox" v-else-if="field.type === 'checkbox'" v-model="model[field.key]"></input>
    </slot>
  </slot>
  <slot name="`${field.key}_APPEND`">
   <span hidden></span>
  </slot>
 </div>
</template>

(this is basically a simplified version of a component that generates auto-forms)

This component can be reused like this:

<auto-form
   :fields="someArray"
   :model="someObject"
>
   <template slot="Name_PREPEND"> This goes before the name field </template>
   <template slot="Name_FIELD"> This content is not showing up; it defaults to the slot markup</template>
   <template slot="Name_APPEND"> This goes after the name field </template>
</auto-form>

However, the slot "${field.key}_FIELD" is not rendered with the above markup using <auto-form>.

When modifying the inner markup of the _PREPEND field as follows:

<slot name="`${field.key}_PREPEND`">
   <span hidden>
     <slot name="`${field.key}_CONTENT`"></slot>
   </span>
 </slot>

The _PREPEND slot cannot be overridden (but _CONTENT can).

Is this a constraint of Vue component slots? Are nested component slots not supported?

In this scenario, this limitation prevents a developer from overriding both the control and label simultaneously via the _FIELD slot (I wanted to add logic to make a field conditional based on other values in the form)

Answer №1

If anyone happens to come across this same issue, it's a tricky one to spot.

It seems that when you apply conditional rendering to markup meant to replace a slot, the default slot will display instead if the conditional rendering is not met.

The best solution is to use v-show instead of v-if when attempting to replace a component slot.

(Not related to the initially suspected issue of nested component slots)

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

Customize your MUI Select to show the selected value in a different way within the Input field

I have a collection of objects and I am looking to link multiple attributes of the object in MenuItem, but I only want one attribute to be displayed in Select https://i.sstatic.net/kDKLr.png The image above displays "10-xyz" in the select display, when i ...

Instructions for utilizing "hasPermissionTo" within a Vue template

I am struggling with my code and I am unsure how to utilize hasPermissionTo in a Vue template to verify if a role has permission to perform a certain action. <script setup> import { Link } from "@inertiajs/inertia-vue3" import { defineProps ...

Ways to change the visibility of a view depending on a state in vuex?

If I have a button <Button> Log me in! </Button>, and I want to dynamically change its style based on the state of my Vuex app (state.user is not null). How should I properly implement this functionality? I'm considering creating a field ...

Can you explain the significance of the regular expression pattern /(?:^|:|,)(?:s*[)+/g in Javascript?

When it comes to Jquery, the regexp pattern is defined as follows: var rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g; This particular pattern is designed to match strings such as "abc,[" and "abc:[", while excluding instances like "abc^[". But what doe ...

The error message "AWS Lambda Node 18: fetch is not defined, please resolve or include global fetch" indicates that

Seeking assistance with calling the Pagespeed Insights API from an AWS Lambda using Node 18. Struggling to make fetch() function properly. Cloudwatch logs indicate the message "inside the try about to fetch," but then nothing else appears. The Lambda con ...

What is the reason behind using 'dummy' local variables to define return object keys?

I've come across a code similar to the one below on several occasions recently. One thing to observe is that modelMapper, viewMapper, and source are all defined as local variables but are not used anywhere else, except for being keys in the return ob ...

Utilize a React Switch Toggle feature to mark items as completed or not on your to-do list

Currently, I am utilizing the Switch Material UI Component to filter tasks in my list between completed and not completed statuses. You can view the demonstration on codesandbox The issue I'm facing is that once I toggle a task as completed, I am un ...

JavaScript or jQuery for filtering table rows

I have limited experience with JavaScript and jQuery, but I am working with a table containing various entries. My goal is to implement a filtering system using a list on the left side of the table. Here is an example I put together: http://jsfiddle.net/B ...

Rotating 3D cube with CSS, adjusting height during transition

I'm attempting to create a basic 2-sided cube rotation. Following the rotation, my goal is to click on one of the sides and have its height increase. However, I've run into an issue where the transition occurs from the middle instead of from the ...

Retrieving an Instance of Google Maps Object with the Help of JQuery

I'm currently working on a script that, when executed, will retrieve an instance of a Google Maps object from the webpage using JQuery. For example, if there is a map present on the page, it will be structured like this: <div class="map">....& ...

What is the proper method to set up jQuery in scripts loaded via AJAX? I keep receiving the error message: 'Uncaught TypeError: Object #<Object> has no method'

I have developed a website with an index page that contains a div for loading the content of each individual page. Initially, I included external JS files and performed initializations within each separate page. However, most of the time when the page loa ...

Displaying gratitude message using AJAX and executing PHP script

I've created a form where I want the "thank you" message to be displayed after submission and also run a PHP script to insert data into the database. However, currently, although the values are being passed correctly via the URL, the upis.php script i ...

Glitch in transmitting server data to client in MERN stack development

I am currently developing a MERN web application and I've encountered a bug in which the data retrieved from the server is not matching what is expected when making a GET request on the client side. Below is the controller function on the server for ...

Display or conceal image based on date

I'm interested in showing a unique zodiac sign image on my website according to the date of the year. For instance, from March 21st to April 21st, I want to display the "Aries" image; from April 22nd to May 21st, I want to show the "Taurus" image, and ...

Obtaining rotate3d values using JavaScript / jQuery

When dealing with an element that has a transformation like the following: style="transform: rotate3d(1, 0, 0, -50deg);" I am looking to extract the value -50 using Javascript or jQuery. It's essential for me to get the absolute value, so negative d ...

Angular has trouble displaying information across multiple HTML files

I have created an HTML file to display some data. The initial HTML file named disc-log.html looks like this: <div> <h2>Discs</h2> <jhi-alert></jhi-alert> <div class="container-fluid"> <div class=" ...

Executing a Particular Function in PHP with Arguments

As a newcomer to PHP and JavaScript, I'm attempting to invoke a specific function in a PHP file from JavaScript. Here is my code: <script> function load($dID) { $.ajax({ url: "myPHP.php", ...

The HTML Canvas seems to be malfunctioning for some unknown reason

As a beginner in programming, I am struggling to understand why the code below is not working. The first three lines of the script are necessary for another part of the webpage, but I don't see how that would affect the rest of the code: <!DOCTY ...

using angular.js to assign a value to an <input> field

There seems to be an issue with the code, as I am unable to see the value in the HTML: '<div ng-app="BusinessinfoModule" ng-controller="BusinessinfoController" <label>Your Business Name</label> <input ...

Guide on how to retrieve a single document (mongoose/mongoDB)

Storing data in a database is crucial for many applications. { "_id": "62fa5aa25778ec97bc6ee231", "user": "62f0eb5ebebd0f236abcaf9d", "name": "Marketing Plan", "columns": [ { ...