Replies: 5 comments 5 replies
-
I second this. |
Beta Was this translation helpful? Give feedback.
-
need this so much😭 |
Beta Was this translation helpful? Give feedback.
-
This would be great |
Beta Was this translation helpful? Give feedback.
-
My hacky solution: Call like this: <div v-scope="LayoutComponent()" @vue:mounted="mounted($el)">
<slot class="hidden">
<p>Slot contents</p>
</slot>
</div> Component: <script>
const LayoutComponent = () => ({
$template: '#layout-template',
slot: null,
mounted(el) {
this.slot = el.querySelector('slot').innerHTML;
},
});
</script>
<template id="layout-template">
<div class="container">
<h2>My Layout</h2>
<div v-html="slot"></div>
</div>
</template> |
Beta Was this translation helpful? Give feedback.
-
I also came up with a solution using nested templates using v-scope: <template id="sharedtemplate">
<h1>Stuff all instances of the component share...</h1>
<slot v-scope="FillSlot(slot)"></slot>
</template>
<div v-scope="MyComponent('#first')">
<template id="first">
<p>This is the slot content for the first component</p>
</template>
</div>
<div v-scope="MyComponent('#another')">
<template id="another">
<p>This is the slot content of the second</p>
</template>
</div> import { createApp } from 'https://unpkg.com/petite-vue?module';
function MyComponent(slot) {
return {
$template: '#sharedtemplate',
slot
}
}
function FillSlot(slot) {
return {
$template: slot
}
}
createApp({
MyComponent,
FillSlot,
}).mount("#app") The benefit of this solution is that you can easily define other Components with multiple slots to be filled (and all can use the same "FillSlot()" function still), the disadvantage is you always have to use ids which could get annoying when creating many instances of your component. |
Beta Was this translation helpful? Give feedback.
-
slots can be usefull to compose components and are more usefull in some than props/attributes
Is there a plan to support slots and named slots ?
Beta Was this translation helpful? Give feedback.
All reactions