I have 3 components: Form
, Card
and Button
.
First, my 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>
Here is my 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 last, here is my 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 question is, how could I pass the text from the Form.vue
to the Button.vue
child component?
2
Answers
Your code would actually be correct. However, if you want to pass a variable to the component in an HTML attribute, instead of using
text=
, you should use:text=
.And instead of passing the
{ }
object, both theCard.vue
andButton.vue
props expect a String. So, instead of an object, pass the specific string as follows::text="myVariableWithStringValue"
Card.vue
Form.vue
Vue3 SFC Playground
Seems you are overusing component and slot props, you could use just named slots:
App.vue
Card.vue:
Button.vue: