admin管理员组文章数量:1389750
I'm trying to understand how/if I can define some sort of slot inheritance in Vue.js v3. I have a Container
class that defines 2 slots: title
and items
. I extend Container
in my Grid
class, and I define the items
slot in there. When I go to use my Grid
class, I would like to define the title
slot. Fiddle for reference.
Container.vue
<template>
<div>
<header v-if="showTitle">
<slot name="title" />
</header>
<main v-if="showItems">
<slot name="items" />
</main>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
name: "MyContainer",
puted: {
showTitle() {
return !!this.$slots.title;
},
showItems() {
return !!this.$slots.items;
},
},
});
</script>
Grid.vue
<template>
<MyContainer>
<template #items>
<span>Here are my items</span>
</template>
</MyContainer>
</template>
<script lang="ts">
import { defineComponent } from "vue";
import MyContainer from "@/ponents/base/Container";
export default defineComponent({
name: "MyGrid",
extends: MyContainer,
ponents: { MyContainer },
});
</script>
App.vue
<template>
<div id="app">
<MyGrid>
<!-- How can I pass this along to MyGrid's base class? -->
<template #title>
<span>This is my title!</span>
</template>
</MyGrid>
</div>
</template>
<script>
import MyGrid from "@/ponents/base/Grid";
export default {
name: "App",
ponents: {
MyGrid,
},
};
</script>
The issue is in App.vue
where I have the ment in the template--I would like to pass along defining the slot in there. Is this possible?
I'm trying to understand how/if I can define some sort of slot inheritance in Vue.js v3. I have a Container
class that defines 2 slots: title
and items
. I extend Container
in my Grid
class, and I define the items
slot in there. When I go to use my Grid
class, I would like to define the title
slot. Fiddle for reference.
Container.vue
<template>
<div>
<header v-if="showTitle">
<slot name="title" />
</header>
<main v-if="showItems">
<slot name="items" />
</main>
</div>
</template>
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
name: "MyContainer",
puted: {
showTitle() {
return !!this.$slots.title;
},
showItems() {
return !!this.$slots.items;
},
},
});
</script>
Grid.vue
<template>
<MyContainer>
<template #items>
<span>Here are my items</span>
</template>
</MyContainer>
</template>
<script lang="ts">
import { defineComponent } from "vue";
import MyContainer from "@/ponents/base/Container";
export default defineComponent({
name: "MyGrid",
extends: MyContainer,
ponents: { MyContainer },
});
</script>
App.vue
<template>
<div id="app">
<MyGrid>
<!-- How can I pass this along to MyGrid's base class? -->
<template #title>
<span>This is my title!</span>
</template>
</MyGrid>
</div>
</template>
<script>
import MyGrid from "@/ponents/base/Grid";
export default {
name: "App",
ponents: {
MyGrid,
},
};
</script>
The issue is in App.vue
where I have the ment in the template--I would like to pass along defining the slot in there. Is this possible?
2 Answers
Reset to default 6According to this, I would have to define a template that essentially passes on any slots defined on the instance. So in my Grid.vue
class, I would add this code:
<template>
<MyContainer>
<template #items>
<span>Here are my items</span>
</template>
<!-- Added this template -->
<template v-for="(_, name) in $slots" v-slot:[name]="slotData">
<slot :name="name" v-bind="slotData" />
</template>
</MyContainer>
</template>
<script lang="ts">
import { defineComponent } from "vue";
import MyContainer from "@/ponents/base/Container";
export default defineComponent({
name: "MyGrid",
extends: MyContainer,
ponents: { MyContainer },
});
</script>
In Grid
ponent define the title slot then inside it render its children dynamically using ponent
:
<MyContainer>
<template #title>
<ponent v-for="(el, i) in $slots.title()" :is="el" :key="i">
</ponent>
</template>
<template #items>
<span>Here are my items</span>
</template>
</MyContainer>
</template>
DEMO
本文标签: javascriptSlot Inheritance in Vuejs 3Stack Overflow
版权声明:本文标题:javascript - Slot Inheritance in Vue.js 3 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744735032a2622275.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论