Passing props to component elements through slots in Vue.js

Trying to pass props from a component element that includes a slot

PatientBooking.vue

<user-profile :titlename="BOOKINGDETAIL">
  <div class="block">
    <div>Ereferral: 84884jjd</div>
    <div>Gender: Male</div>
    <div>Height: 84</div>
  </div>
</user-profile>

UserProfile.vue

<div class="block">
  <div class="block">
    <template v-if="titlename == 'BOOKINGDETAIL'">BOOKING DETAIL</template>
    <template v-else-if="titlename == 'BOOKING'">BOOKING</template>
    <template v-else>RESULT DETAIL</template>
  </div>
  <div class="block">
    <slot></slot>
  </div>
</div>

export default {
  name: "UserProfile",
  props: {
    titlename: {
      type: String,
      default: ""
    }
  }
}  

When binding the user-profile component with titlename props, it always defaults to an empty string and displays RESULT DETAIL. Need assistance in solving this issue.

Answer №1

Utilize scoped slots in the following manner :

UserProfile.vue

<div class="block">
  <div class="block">
    <template v-if="titlename == 'BOOKINGDETAIL'">BOOKING DETAIL</template>
    <template v-else-if="titlename == 'BOOKING'">BOOKING</template>
    <template v-else>RESULT DETAIL</template>
  </div>
  <div class="block">
    <slot :title="titlename"></slot>
  </div>
</div>

PatientBooking.vue

<user-profile :titlename="BOOKINGDETAIL">
 <template v-slot:default="slotProps">
  <div class="block">
   // utilize slotProps.title to access titlename provided via slots
    <div>Ereferral: 84884jjd</div>
    <div>Gender: Male</div>
    <div>Height: 84</div>
  </div>
</template>
</user-profile>

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

Having trouble rendering a dynamic table with JavaScript utilizing a JSON object

I am struggling to retrieve data in JSON format and display it in a table. Despite trying various methods, I have been unable to make it work. Below is the code for this function. Can someone please assist me in identifying what is wrong with it? As of now ...

Image failed to load

Issue Encountered in Browser Console: https://static.food2fork.com/pastaallavodkaa870.jpg.jpg 404 While attempting to display the image on the browser, I am uncertain if the problem lies within my code or with food2fork. Code from index.js: // Alway ...

The React Router's Switch component fails to update when the route is changed

In my component, I have a list of news categories. The links to these categories are in another component within the Router. However, when I change the link, the content does not update. I suspect this is because my NewsFeed component, where I define the S ...

Angular controller unit testing is an essential practice in software development

I am currently working with an angular module named 'widgets'. In my app, I have used its signature as follows: var app = angular.module('widgets', [ 'widget.Panel', 'widget.List', 'services' ] ...

Viewing the JSON Data

Support: $scope.createTimeSlots = function(interval, field) { var startingTime = moment().hours(8).minutes(0); field.timeslots = []; for (var i = 0; i < interval; i++) { $scope.intervals = 60; field.timeslots.push(startingTi ...

Launching a Vue.js application using Firebase Hosting

What's the most effective method for deploying a vue.js app that was initially created with vue-cli to Firebase Hosting? After preparing the app for deployment using npm run build A "dist" folder is generated containing the bundled js file and asset ...

Tiny adjustment to jQuery Collapsible Panel

I have successfully implemented a jQuery Accordion in my demo. When I click on 'About Us', it reveals the 'Team' link below. Exciting! Now, I am wondering if it is possible for this Accordion to function without needing the 'item& ...

What is the best way to eliminate a specific value within a flatmap?

This is the flatMap: const choices = names.flatMap( (item) => item.name + " - " + item.size + "- " + item.category ); console.log(choices): https://i.stack.imgur.com/MO4b1.png If the item.category is equal to S-XL, how can ...

How can JavaScript be properly embedded using PhantomJS?

My objective is to insert the following code snippet into a website using PhantomJS: javascript document.getElementById("pickupZip").value = "90049"; document.getElementById("refreshStoresForZipPop").click(); After attempting this in my inject.js file, I ...

Redirecting JavaScript form to search engine

I am struggling with creating a form that enables a user to input text and then directs them to a specified search engine with the input as the query. I am encountering difficulties in getting the JavaScript to properly redirect. An interesting observatio ...

Javascript is coming back with a message of "Access-Control-Allow-Origin" not being allowed

I've been encountering some unusual challenges with my React application related to the Access-Control-Allow-Origin issue. Initially, I had similar issues with the login and registration system which I thought were resolved, but now I'm facing di ...

Discovering the Windows Identifier of the Opener Window in Chrome via JavaScript

I recently opened a link in a new window and realized that the current window's ID is now the ID of the newer window. I'm curious if there is any method to determine the window ID of the original window (the opener) from the perspective of the ne ...

Thinking of hosting an event centered around Google Maps?

Are there specific event listeners for panning or zooming the Google Map, in addition to the mouseover, mouseout, and click events? UPDATE: I tried using 'center_changed', but it didn't work the way I expected. Whenever I move the mouse ov ...

Error: The function 'stepUp' was invoked on an object lacking the HTMLInputElement interface during an AJAX request

It's always frustrating to have to ask a question that has already been asked, but I'm having trouble finding a solution that works for me. My issue involves retrieving the value of an input and sending it via AJAX. $("#cell_number").on("change" ...

Transforming JSON objects, retrieve an empty value in place of undefined

Can someone please offer some advice on the following: I am retrieving JSON data using this code snippet: $.getJSON(jsonPath, function(returnedData){ ... }); The JSON object returned will have a structure similar to this: ... "skin": { "elapsedTextCo ...

Place an image at the top of the canvas at a specific location

Currently, I am in the process of reconstructing this specific website My approach involves working with React (similar to the aforementioned site) and utilizing the same cropper tool that they have implemented. For cropping, I am incorporating react-imag ...

Encountering the error "Vue 2.0 encounters an undefined push during router.push

I'm currently working on a project that involves implementing a function to route the list of subMenus displayed by the mainMenu. Each subMenu is identified by its specific name, which I would like to append and use as the route path that I am aiming ...

Is there a way to control a single Angular application using multiple JavaScript scripts?

Summary: I am looking for a way to allow two script files to handle different tasks within the same angular app. One needs to initialize the app, while the other needs to assign a templateCache object to a section of JSON stored in localStorage. Backgrou ...

Mastering the utilization of custom input events in PrimeNG with Angular

I am currently working on an Angular 16 project. Within this project, I have implemented a number input field that is being validated using formControls. To make my work more efficient, especially since this input is used frequently, I decided to encapsula ...

How can we accurately identify the server that initiated an AJAX call in a secure manner?

Imagine a scenario where Site A embeds a JavaScript file from Server B and then makes a JSONP or AJAX request to a resource on Server B. Is there any foolproof way for Server B to determine that the specific JSONP request originated from a user on Site A, ...