// Component.vue
const block = (
ββ<div
ββββclass="block"
ββββonClick="handleClick"
ββββ<!-- here may go other prop definitions, perhaps complex ones -->
ββ>
ββββBlock that should be reused
ββ</div>
);
return (
ββ<div class="container">
ββββ<div class="header">
ββββββHeader content
ββββββ{!isCompact && block}
ββββ</div>
ββββ<div class="footer">
ββββββFooter content
ββββββ{isCompact && block}
ββββ</div>
ββ</div>
); // Component.vue
<template>
ββ<div class="container">
ββββ<div class="header">
ββββββHeader content
ββββββ<div v-if="!isCompact"
ββββββββclass="block"
ββββββββ@click="handleClick">
ββββββββBlock that should be reused
ββββββ</div>
ββββ</div>
ββββ<div class="footer">
ββββββFooter content
ββββββ<div v-if="isCompact"
ββββββββclass="block"
ββββββββ@click="handleClick">
ββββββββBlock that should be reused
ββββββ</div>
ββββ</div>
ββ</div>
</template> npm i @vueuse/core // Component.vue
<script setup lang="ts">
ββimport { createReusableTemplate } from '@vueuse/core'
ββconst [DefineTemplate, ReuseTemplate] = createReusableTemplate()
</script> // Component.vue
<template>
ββ<DefineTemplate>
ββββ<div
ββββββclass="block"
ββββββ@click="handleClick">
ββββββBlock that should be reused
ββββ</div>
ββ</DefineTemplate>
ββ<div class="container">
ββββ<div class="header">
ββββββHeader content
ββββββ<ReuseTemplate v-if="!isCompact" />
ββββ</div>
ββββ<div class="footer">
ββββββFooter content
ββββββ<ReuseTemplate v-if="isCompact" />
ββββ</div>
ββ</div>
</template> // Component.vue
<template>
ββ<DefineTemplate>
ββββ<div>Reused block A</div>
ββ</DefineTemplate>
ββ<ReuseTemplate />
ββ<!-- registers another template instead -->
ββ<DefineTemplate>
ββββ<div>Reused block B</div>
ββ</DefineTemplate>
ββ<!-- will now render new template -->
ββ<ReuseTemplate />
</template>
// Output:
// Reused block A
// Reused block B // Component.vue
<script setup>
ββimport { createReusableTemplate } from '@vueuse/core'
ββconst [DefineBlockA, ReuseBlockA] = createReusableTemplate()
ββconst [DefineBlockB, ReuseBlockB] = createReusableTemplate()
</script>
<template>
ββ<DefineBlockA>
ββββ<div>Reused block A</div>
ββ</DefineBlockA>
ββ<DefineBlockB>
ββββ<div>Reused block B</div>
ββ</DefineBlockB>
ββ<div class='text-comment'><!-- the two can exist simultaneously --></div>
ββ<ReuseBlockA />
ββ<ReuseBlockB />
</template>
// Output:
// Reused block A
// Reused block B
β // Component.vue
const renderBlock = (className: string) => (
ββ<div
ββββclass={`block ${className}`}
ββββonClick="handleClick">
ββββBlock that should be reused and customized.
ββ</div>
);
return (
ββ<div class="container">
ββββ<div class="header">
ββββββHeader content
ββββββ{!isCompact && renderBlock("full")}
ββββ</div>
ββββ<div class="footer">
ββββββFooter content
ββββββ{isCompact && renderBlock("compact")}
ββββ</div>
ββ</div>
); // Component.vue
<script setup lang="ts">
ββimport { createReusableTemplate } from '@vueuse/core'
ββconst [DefineTemplate, ReuseTemplate] = createReusableTemplate<{ className: string }>()
</script> // Component.vue
<template>
ββ<DefineTemplate v-slot="{ className }">
ββββ<div
ββββββ:class="['block', className]"
ββββββ@click="handleClick">
ββββββBlock that should be reused and customized.
ββββ</div>
ββ</DefineTemplate>
ββ<div class="container">
ββββ<div class="header">
ββββββHeader content
ββββββ<ReuseTemplate v-if="!isCompact" className="full" />
ββββ</div>
ββββ<div class="footer">
ββββββFooter content
ββββββ<ReuseTemplate v-if="isCompact" className="compact" />
ββββ</div>
ββ</div>
</template>
β // Component.vue
const renderBlock = (className: string, content: ReactNode) => (
ββ<div
ββββclass={`block ${className}`}
ββββonClick="handleClick">
ββββBlock that should be reused with custom content.
ββββCustom content: {content}
ββ</div>
);
return (
ββ<div class="container">
ββββ<div class="header">
ββββββHeader content
ββββββ{!isCompact && renderBlock(
ββββββββ"full",
ββββββββ<div>Full content</div>
ββββββ)}
ββββ</div>
ββββ<div class="footer">
ββββββFooter content
ββββββ{isCompact && renderBlock(
ββββββββ"compact",
ββββββββ<div>Compact content</div>
ββββββ)}
ββββ</div>
ββ</div>
); // Component.vue
<template>
ββ<DefineTemplate v-slot="{ className, $slots }">
ββββ<div
ββββββ:class="['block', className]"
ββββββ@click="handleClick">
ββββββBlock that should be reused with custom content.
ββββββCustom content:
ββββββ<!-- To render the slot -->
ββββββ<component :is="$slots.default" />
ββββ</div>
ββ</DefineTemplate>
ββ<div class="container">
ββββ<div class="header">
ββββββHeader content
ββββββ<ReuseTemplate v-if="!isCompact" className="full">
ββββββββ<div>Full content</div>
ββββββ</ReuseTemplate>
ββββ</div>
ββββ<div class="footer">
ββββββFooter content
ββββββ<ReuseTemplate v-if="isCompact" className="compact">
ββββββββ<div>Compact content</div>
ββββββ</ReuseTemplate>
ββββ</div>
ββ</div>
</template>