App.vue
SlotComp.vue
main.js
<template>
<h3>Slots in Vue</h3>
<p>We create card-like div boxes from the foods array.</p>
<div id="wrapper">
<slot-comp v-for="x in foods">
<img v-bind:src="x.url">
<h4>{{x.name}}</h4>
<p>{{x.desc}}</p>
</slot-comp>
</div>
</template>
<script>
export default {
data() {
return {
foods: [
{ name: 'Apple', desc: 'Apples are a type of fruit that grow on trees.', url: 'img_apple.svg'},
{ name: 'Pizza', desc: 'Pizza has a bread base with tomato sauce, cheese, and toppings on top.', url: 'img_pizza.svg'},
{ name: 'Rice', desc: 'Rice is a type of grain that people like to eat.', url: 'img_rice.svg'},
{ name: 'Fish', desc: 'Fish is an animal that lives in water.', url: 'img_fish.svg'},
{ name: 'Cake', desc: 'Cake is something sweet that tates good but is not consodered healthy.', url: 'img_cake.svg'}
]
}
}
}
</script>
<style>
#wrapper {
display: flex;
flex-wrap: wrap;
}
#wrapper img {
display: block;
margin: auto;
width: 60%;
}
</style>
<template>
<div>
<slot></slot>
</div>
</template>
<script></script>
<style scoped>
div {
background-color: lightgreen;
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: 20px 10px 0 10px;
}
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')