admin管理员组文章数量:1278950
Hello I have a Child ponent that the main function is to filter and render a list of items. This child ponent is to be used in multiple parent ponents(views) and depending on the parent ponent the child ponent need to render a different child ponent (grand child).
Parent Component
<template>
<main>
//Child Component
<list-ponent
name="my items"
//List of Items I need to render
:list="items.list"
>
//Slot Passing my grandchild ponent
<template slot="child-ponent">
<ponent :is="child_ponent" :items="item"></ponent>
</template>
</list-ponent>
</main>
</template>
<script>
import ListComponent from '.ListComponent';
import ItemComponent from '.ItemComponent.vue';
export default {
ponents: {
ListComponent,
ItemComponent
},
data() {
return {
child_ponent: 'ItemComponent'
};
},
}
</script>
ListComponent.vue (child ponent)
<template>
<main>
<v-row class="ma-0">
<v-col v-for="(item, index) in list" :key="index" class="pa-0">
// I would like render my grandchild ponent here.
<slot name="child-ponent"></slot>
</v-col>
</v-row>
</main>
</template>
<script>
export default {
props: {
name: {
type: String,
required: true
},
list: {
type: Array,
required: true
}
}
};
</script>
ItemComponent.vue (grand child)
<template>
<div>
<v-img
src="item.image"
></v-img>
<v-row>
<span>{{
item.name
}}</span>
</v-row>
</div>
</template>
<script>
export default {
props: {
item: {
type: Object,
required: true
}
}
}
</script>
So basically I need to be able to pass ItemComponent.vue(grandchild) from the View(grandfather) to the View's ListComponent.vue(child) so that the child ponent can loop trough the items passed from the parent and use the grand child to render the information.
Also where do I declare the props for the grandchild?
Hello I have a Child ponent that the main function is to filter and render a list of items. This child ponent is to be used in multiple parent ponents(views) and depending on the parent ponent the child ponent need to render a different child ponent (grand child).
Parent Component
<template>
<main>
//Child Component
<list-ponent
name="my items"
//List of Items I need to render
:list="items.list"
>
//Slot Passing my grandchild ponent
<template slot="child-ponent">
<ponent :is="child_ponent" :items="item"></ponent>
</template>
</list-ponent>
</main>
</template>
<script>
import ListComponent from '.ListComponent';
import ItemComponent from '.ItemComponent.vue';
export default {
ponents: {
ListComponent,
ItemComponent
},
data() {
return {
child_ponent: 'ItemComponent'
};
},
}
</script>
ListComponent.vue (child ponent)
<template>
<main>
<v-row class="ma-0">
<v-col v-for="(item, index) in list" :key="index" class="pa-0">
// I would like render my grandchild ponent here.
<slot name="child-ponent"></slot>
</v-col>
</v-row>
</main>
</template>
<script>
export default {
props: {
name: {
type: String,
required: true
},
list: {
type: Array,
required: true
}
}
};
</script>
ItemComponent.vue (grand child)
<template>
<div>
<v-img
src="item.image"
></v-img>
<v-row>
<span>{{
item.name
}}</span>
</v-row>
</div>
</template>
<script>
export default {
props: {
item: {
type: Object,
required: true
}
}
}
</script>
So basically I need to be able to pass ItemComponent.vue(grandchild) from the View(grandfather) to the View's ListComponent.vue(child) so that the child ponent can loop trough the items passed from the parent and use the grand child to render the information.
Also where do I declare the props for the grandchild?
Share Improve this question asked Apr 10, 2020 at 2:22 Angel D.Angel D. 2253 silver badges13 bronze badges1 Answer
Reset to default 11I was able to find a solution after all I will leave this for those who need it.
basically in the child ponent we need to give access to the attribute to the parent trough the slot by binding the item like:
<slot name="child-ponent" :item="item"></slot>
and on the parent we can access it by binding the slot and giving a name to the object in this case I chose child and notice that on the ponent we can access item by declaring child.item
<template v-slot:child-ponent="child">
<ponent :is="child_ponent" :itinerary="child.item"></ponent>
</template>
本文标签: javascripthow to pass components as props in vue js and how to use it properlyStack Overflow
版权声明:本文标题:javascript - how to pass components as props in vue js and how to use it properly? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741245216a2364744.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论