admin管理员组文章数量:1405170
Child ponent:
Vueponent('v-data', {
template: `
<div class="v-data">
<slot :visible-data="visibleData"></slot>
</div>
`,
puted: {
visibleData(){
return [1,2,3];
},
},
});
In parent ponent I am using it like this:
<v-data>
<li v-for="x in visibleData">{{x}}</li>
</v-data>
but visibleData
is not passed in the template. I should be able to get 1,2,3.
Is there a way to pass variables between ponents like this?
Child ponent:
Vue.ponent('v-data', {
template: `
<div class="v-data">
<slot :visible-data="visibleData"></slot>
</div>
`,
puted: {
visibleData(){
return [1,2,3];
},
},
});
In parent ponent I am using it like this:
<v-data>
<li v-for="x in visibleData">{{x}}</li>
</v-data>
but visibleData
is not passed in the template. I should be able to get 1,2,3.
Is there a way to pass variables between ponents like this?
Share Improve this question asked Apr 6, 2020 at 18:04 AlexAlex 66.1k185 gold badges459 silver badges651 bronze badges 1- In Vue data gets passed down not up. You might be able to create a puted property on your parent that just returns the property from the child. This probably won't work but it's worth a shot. – Adam H Commented Apr 6, 2020 at 18:22
2 Answers
Reset to default 4You are definitely looking for Scoped Slots
. With this, one can easily pass data from the child ponent to the parent such that data could be referred to in the template being passed on to the child ponent. You can use it to pass data from your child ponent as
<div class="v-data">
<slot :visible-data="visibleData"></slot>
</div>
Which can be referred in the parent as
<v-data>
<template #default="{visibleData}">
<li v-for="(value, index) in visibleData" :key="index">{{value}}</li>
</template>
</v-data>
Few things to note here
The properties can be referred to using the name of the slot. Here we are referring to the default slot thus using the keyword
default
.We can use
Object Destructuring
to directly access the property passed to the parent ponent
Sanbox present HERE
What you need is Scoped Slots
So that parent can see the child's data.
<v-data>
<template v-slot:default="slotProps">
<li v-for="x in slotProps.visibleData">{{x}}</li>
</template>
</v-data>
I might have made some mistake here, but I'd remend reading it here: Official Docs
本文标签: javascriptPass variables through ltslotgt to parent componentStack Overflow
版权声明:本文标题:javascript - Pass variables through <slot> to parent component - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744254686a2597422.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论