App.vue
SlotComp.vue
main.js
<template>
<h2>Re-usable Slot Cards</h2>
<p>We create card-like div boxes from the foods array.</p>
<p>We also create a card-like footer by reusing the same component.</p>
<div id="wrapper">
<slot-comp v-for="x in foods">
<img v-bind:src="x.url">
<h4>{{x.name}}</h4>
</slot-comp>
</div>
<footer>
<slot-comp>
<h3>Footer</h3>
</slot-comp>
</footer>
</template>
<script>
export default {
data() {
return {
foods: [
{ name: 'Apple', url: 'img_apple.svg'},
{ name: 'Pizza', url: 'img_pizza.svg'},
{ name: 'Rice', url: 'img_rice.svg'},
{ name: 'Fish', url: 'img_fish.svg'},
{ name: 'Cake', url: 'img_cake.svg'}
]
}
}
}
</script>
<style>
#wrapper {
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
#wrapper > div {
background-color: lightgreen;
}
footer > div {
background-color: lightpink;
}
#wrapper img {
display: block;
margin: 20% auto 0;
width: 60%;
}
h3, h4 {
text-align: center;
}
</style>
<template>
<div>
<slot></slot>
</div>
</template>
<script></script>
<style scoped>
div {
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
transition: 0.3s;
flex-basis: 150px;
border-radius: 10px;
border: solid black 2px;
margin: 10px;
padding: 0 10px 0;
}
div:hover {
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
}
</style>
import { createApp } from 'vue'
import App from './App.vue'
import SlotComp from './components/SlotComp.vue'
const app = createApp(App)
app.component('slot-comp', SlotComp)
app.mount('#app')