Exploring Vue.js: Navigating through child components from parent component

I currently have 3 components in my project: Form, Card, and Button.

Let's start with the Button.vue component:

<template>
  <div>
    <slot v-bind="{ text }">
      <button>{{ text }}</button>
    </slot>
  </div>
</template>

<script setup>
const props = defineProps({
  text: {
    type: String,
    required: true
  }
});
</script>

Next, we have the Card.vue component:

<template>
  <Button text="{ text }">
    <template #default="{ text }">
      <button>{{ text }}</button>
    </template>
  </Button>
</template>

<script setup>
import Button from './Button.vue'

const props = defineProps({
  text: {
    type: String,
    required: true
  }
});
</script>

And finally, here is the Form.vue component:

<template>
  <div>
    <Card text="Some text">
      <template #default="{ text }">
      </template>
    </Card>
  </div>
</template>

<script setup>
import Card from './Card.vue'
</script>

My main inquiry now is: How can I successfully transfer the text data from the Form.vue to its child component Button.vue?

Answer №1

Your code seems correct, but if you intend to pass a variable to the component through an HTML attribute, make sure to use :text= instead of text=.

Both the Card.vue and Button.vue props require a String input, so ensure you pass a string value like this:

:text="myVariableWithStringValue"

Card.vue

<template>
  <Button :text="buttonText"> <!-- Remember to use ":text" instead of "text" -->
    <template #default="{ text }">
      <button>{{ text }}</button>
    </template>
  </Button>
</template>

<script setup>
import Button from './Button.vue'

const props = defineProps({
  // I changed the prop name from 'props.text' to 'props.buttonText' for clarity
  buttonText: {
    type: String,
    required: true
  }
});
</script>

Form.vue

<template>
  <div>
    <!-- Change 'props.text' to 'props.buttonText' for consistency -->
    <!-- To pass text from a variable, use :buttonText="variableName" -->
    <!-- Now, set 'Some text' as the buttonText string directly -->
    <Card buttonText="Some text"></Card>
    <!-- You can't utilize <template> in <Card> because it lacks a defined <slot> in Card.vue's template -->
  </div>
</template>

<script setup>
import Card from './Card.vue'
</script>

Answer №2

Check out this cool VueJS Playground

Looks like you may be using too many component and slot props, consider simplifying with named slots:

MainApp.vue

<template>
  <div>
    <Card>
      <template #title>Title of the card</template>
      Sample card content
      <template #button>Click me!</template>
    </Card>
  </div>
</template>

<script setup>
import Card from './CardComponent.vue'

</script>

CardComponent.vue:

<template>
    <h2><slot name="title"></slot></h2>
    <p>
    <slot></slot>
    </p>
    <Button>
        <slot name="button"></slot>
    </Button>
</template>

<script setup>
import Button from './ButtonComponent.vue'
</script>

ButtonComponent.vue:

<template>
    <div>
      <button><slot></slot></button>
    </div>
</template>

<script setup>
</script>

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

Ensure that the date range picker consistently shows dates in a sequential order

Currently utilizing the vuetify date range picker component https://i.stack.imgur.com/s5s19.png At this moment, it is showcasing https://i.stack.imgur.com/GgTgP.png I am looking to enforce a specific display format, always showing the lesser date first ...

Using Vuetify to create subtle fade in animation when scrolling

I'm attempting to create a fade-in effect while scrolling through the page for the first time. Even though I used <v-scroll-x-transition appear>, the desired effect doesn't seem to be working. Instead of fading in from the right as expecte ...

Encountering a problem when trying to map a List with a button in ReactJS

As I map results from a GET request, everything displays correctly - each User's picture, name, and the correct label on the button. However, the issue arises when I try to access specific user information by clicking on the button; instead of retriev ...

Run the script within the Angular scope

Hey there! I'm having an issue passing a JavaScript code through Angular scope, but when it renders on the view page, it's displayed as text. I even attempted using ng-Sanitize, but unfortunately, that didn't solve the problem. &lt;div ...

Retrieving the JSON value from a data attribute and then locating the corresponding JSON key in a designated element within the DOM

I have an HTML element that contains a data attribute: <a href="#" data-trigger="{ "rem": "albatros", "ap":1 }'">Remove</a> <div data-container> <p>lorem ipsum<p> <p data-rem></p> </div> 1. So ...

Quasar framework's autocomplete feature failing to display text in dropdown list

I'm currently utilizing the most recent version of quasar (0.17) and I am attempting to implement the autocomplete functionality. Although I can successfully filter the list and choose a value, the text in the autocomplete list is not being displayed: ...

How to handle Component binding change events in AngularJS

I have developed a component in AngularJS that displays data. However, when the customer binding changes, I need to call a service in the component controller, but it is not updating. Below is the code snippet: In my Test1.html file: <tab-customer tit ...

Attempting to store a specific h1 text.event as a variable, allowing it to be saved upon input of the initial letter

After typing the second key, you can continue to input more characters as desired. It is possible to customize the text displayed in the h1 using your own name or any other text of your preference without needing a textbox. $(document).keypress(functio ...

Passing a value from the Trigger button to the Modal button in Angular-UI Bootstrap

Seeking some help. I am working with angular-ui-bootstrap alongside php and mysql. My goal is to pass a value from a list of links (dynamically generated from php mysql) to a modal button each time the modal is loaded. HTML // The link below is generated ...

What is preventing images from displaying in my slider within baguetteBox?

Currently, I am in the process of incorporating a slider into my website using the baguetteBox library. After consulting the documentation, I successfully implemented an example that is functioning smoothly without any issues. In order to display a series ...

Unable to access client's channels cache with empty parameters

In my quest to locate the guild and then channel within that guild, I have employed the following code snippet: const guild = client.guilds.cache.find(guild => guild.name === "bla bla bla"); const channel = guild.channels.cache.find(ch => c ...

Using Vue and PHP to parse a JSON string and display it as a table

I am currently facing a challenge in parsing a JSON string from my Laravel application to my Vue view. The format of the JSON string could be as follows: { "1":[ { "row":"Some text here on first column." }, { "row":"And more text. Second row ...

Modifying subtotal values using PHP and JavaScript

I've been working on this code snippet to calculate my subtotal and determine the total payment. Can someone provide some assistance? $viewx = mysql_query("select distinct toorderid from ordercontainer where toordercategory='$ordercategory' ...

Invoking a Jquery Dialog

Every time I click on the button to display a message, I encounter an issue where one MessageBox appears as expected. However, when I open the Jquery Dialog for the second time, two MessageBoxes pop up and the same problem occurs with three MessageBoxes on ...

What is the proper method for securing this?

Trying to retrieve 'this' within a method that is invoked by pressing a button, where this points to both the class and the pressed button: p.myVar = 'apple'; $('.go').on('click', this._init); p._init = function(e ...

When sending API data back to Vue from Express, the response is coming back as an empty

I am encountering an issue with my express app where I am trying to send data back from a third-party API to my front-end application. Despite being able to see the content in my express API, I am not receiving any results on the front end. Any suggestions ...

Is there a way to direct the user's cursor to a specific location on the page when they hover over an image?

I'm interested in creating a script that can manipulate the user's mouse position when they hover over an image. For example, if I hover over the image, I want the mouse to be dragged lower towards a specific text. Is there a method to achieve th ...

What is the method for assigning a class name to a child element within a parent element?

<custom-img class="decrease-item" img-src="images/minus-green.svg" @click="event => callDecreaseItem(event, itemId)" /> Here we have the code snippet where an image component is being referenced. <template&g ...

transferring data from JavaScript to PHP variables

Currently, I am working on a project that involves using ajax to access the database asynchronously. The value retrieved is stored in a JavaScript variable like this: var content=xmlhttp.responseText; My next step is to pass this value to the PHP module ...

Rotate Text in HTML <thead> Tag

I need assistance with rotating a table header. https://i.stack.imgur.com/hcvxx.png The HTML th elements within the thead section are described as follows: <th> <span class="rotate-all">Period</span> </th> <th> < ...