How to pass props using slots from parent to child -vuejs
Solution 1
You need to use a scoped slot. You were almost there, I just added the template that creates the scope.
<my-parent>
<template slot-scope="{signal}">
<my-child :signal="signal"></my-child>
<my-child :signal="signal"></my-child>
</template>
</my-parent>
Here is your code updated.
const MyParent = Vue.component('my-parent', {
template: `<div>
<h3>Parent's Children:</h3>
<slot :signal="parentVal"></slot>
</div>`,
data: function() {
return {
parentVal: 'value of parent'
}
}
});
const MyChild = Vue.component('my-child', {
template: '<h3>Showing child {{signal}}</h3>',
props: ['signal']
});
new Vue({
el: '#app',
components: {
MyParent,
MyChild
}
})
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<div id="app">
<my-parent>
<template slot-scope="{signal}">
<my-child :signal="signal"></my-child>
<my-child :signal="signal"></my-child>
</template>
</my-parent>
</div>
The release of Vue 2.6 introduces a unified v-slot
directive which can be used for normal or scoped slots. In this case, since you're using the default, unnamed slot, the signal
property can be accessed via v-slot="{ signal }"
:
<my-parent>
<template v-slot="{ signal }">
<my-child :signal="signal"></my-child>
<my-child :signal="signal"></my-child>
</template>
</my-parent>
Solution 2
You may try this technique.
In this example. Let assume a parent component wants to share prop foo with value bar.
Parent component
<parent>
<template v-slot:default="slotProps">
// You can access props as object slotObjects {foo"bar"}
<p>{{slotProps.foo}}</p>
</template>
<parent>
Child
<template>
<div class="parent">
<slot :foo="bar" />
</div>
</template>
Got the idea from this video
I hope it helped you accomplish your tasks.
Related videos on Youtube
Kunle
Updated on March 09, 2022Comments
-
Kunle over 1 year
I have a parent component and a child component.
The parent component's template uses a slot so that one or more child components can be contained inside the parent.
The child component contains a prop called 'signal'.
I would like to be able to change data called 'parentVal' in the parent component so that the children's signal prop is updated with the parent's value.This seems like it should be something simple, but I cannot figure out how to do this using slots: Here is a running example below:
const MyParent = Vue.component('my-parent', { template: `<div> <h3>Parent's Children:</h3> <slot :signal="parentVal"></slot> </div>`, data: function() { return { parentVal: 'value of parent' } } }); const MyChild = Vue.component('my-child', { template: '<h3>Showing child {{signal}}</h3>', props: ['signal'] }); new Vue({ el: '#app', components: { MyParent, MyChild } })
<script src="https://unpkg.com/vue/dist/vue.js"></script> <div id="app"> <my-parent> <my-child></my-child> <my-child></my-child> </my-parent> </div>
-
thanksd almost 5 yearslol and just yesterday Vue 2.6 added a new slot syntax, which is what they'll support in v3. So in this case:
v-slot="{ signal }"
. Althoughslot-scope
is still supported. -
Bert almost 5 years@thanksd if I'm reading that right the newer syntax is for naming a slot, not scoped slots?
-
Bert almost 5 years@thanksd or they're combining both into a directive?
-
Bert almost 5 years@thanksd Ah ok, I get it. I'll update the answer later tonight or you can go ahead if you want. medium.com/the-vue-point/vue-2-6-released-66aa6c8e785e
-
mesqueeb over 4 yearsIs there any way I can do this without having to write
v-slot
and:signal
in the "app" file, but directly integrate it in the templates formy-parent
andmy-child
that I only have to add thesignal
prop on the parent in my app, and my children have access to it automatically. -
hariseldon78 almost 4 years@mesqueeb for that usage the best option seems to use dependency injection, see vuejs.org/v2/guide/…
-
V. Rubinetti almost 2 yearsNote, if you have a bunch of props you want to pass down to the child, you can use the syntax
<SomeParent v-slot="props"><SomeChild v-bind="props"/></SomeParent>
. This is basically the equivalent of<Component {...props} />
in React. I always forget the syntax for Vue.