Home > Article > Web Front-end > Understanding Slots in Vue ith Composition API
Slots are a powerful mechanism in Vue that enable components to define content areas that can be customized by the parent component. This promotes reusability and flexibility in building UI components. Vue 3 offers two primary slot types:
In Vue 3, slots allow you to create flexible components by providing a way to pass content into child components. The Composition API enhances how we use slots, making it more intuitive and powerful.
Slots are a way to define placeholder content in a component that can be filled with custom content when the component is used. They help in creating reusable and customizable components.
Here’s how you can define and use slots in a Vue 3 component using the Composition API:
Example of a Default Slot
<template> <div> <slot></slot> </div> </template> <script> import { defineComponent } from 'vue'; export default defineComponent({ name: 'MyComponent', }); </script>
Usage:
<MyComponent> <p>This is some content passed to the default slot!</p> </MyComponent>
<template> <div> <slot name="header"></slot> <slot></slot> <slot name="footer"></slot> </div> </template> <script> import { defineComponent } from 'vue'; export default defineComponent({ name: 'LayoutComponent', }); </script>
Usage:
<LayoutComponent> <template #header> <h1>Header Content</h1> </template> <p>Main Content goes here!</p> <template #footer> <footer>Footer Content</footer> </template> </LayoutComponent>
Scoped slots allow you to pass data from the child component back to the parent.
<template> <div> <slot :message="message"></slot> </div> </template> <script> import { defineComponent, ref } from 'vue'; export default defineComponent({ name: 'MessageComponent', setup() { const message = ref("Hello from the child!"); return { message }; }, }); </script>
Usage:
<MessageComponent> <template #default="{ message }"> <p>{{ message }}</p> </template> </MessageComponent>
Let's create a simple food delivery application using Vue 3 with the Composition API and slots. This example will showcase a main FoodDelivery component that uses slots to display a list of food items, along with a header and footer.
Here’s the FoodDeliverycomponent that accepts named slots for the header, food items, and footer.
<template> <div class="food-delivery"> <slot name="header"></slot> <div class="food-items"> <slot></slot> </div> <slot name="footer"></slot> </div> </template> <script> import { defineComponent } from 'vue'; export default defineComponent({ name: 'FoodDelivery', }); </script> <style> .food-delivery { border: 1px solid #ccc; padding: 20px; border-radius: 8px; } .food-items { margin: 20px 0; } </style>
Next, let’s create a simple FoodItem component to represent individual food products.
<template> <div class="food-item"> <h3>{{ name }}</h3> <p>Price: ${{ price.toFixed(2) }}</p> <button @click="addToCart">Add to Cart</button> </div> </template> <script> import { defineComponent } from 'vue'; export default defineComponent({ name: 'FoodItem', props: { name: { type: String, required: true, }, price: { type: Number, required: true, }, }, methods: { addToCart() { // Logic to add the item to the cart console.log(`${this.name} added to cart!`); }, }, }); </script> <style> .food-item { border: 1px solid #eee; padding: 10px; margin-bottom: 10px; border-radius: 5px; } </style>
Now, let’s put everything together in a parent component that uses our FoodDelivery and FoodItem components.
FoodDelivery Component: This component acts as a layout for our food delivery service. It accepts three slots: a header, a default slot for the food items, and a footer.
FoodItem Component: This represents individual food products. It takes name and price as props and has a method to simulate adding the item to a cart.
<template> <div> <slot></slot> </div> </template> <script> import { defineComponent } from 'vue'; export default defineComponent({ name: 'MyComponent', }); </script>
<MyComponent> <p>This is some content passed to the default slot!</p> </MyComponent>
Let's enhance the scoped slot example to make it clearer and more functional. This example will demonstrate how to use scoped slots to pass item data from a child component to a parent component, allowing for flexible rendering.
This component will display a list of items and use a scoped slot to allow the parent to customize how each item is rendered.
<template> <div> <slot name="header"></slot> <slot></slot> <slot name="footer"></slot> </div> </template> <script> import { defineComponent } from 'vue'; export default defineComponent({ name: 'LayoutComponent', }); </script>
Now, let’s create a parent component that uses the ItemList component and provides a custom template for rendering each item using the scoped slot.
<template> <div> <slot></slot> </div> </template> <script> import { defineComponent } from 'vue'; export default defineComponent({ name: 'MyComponent', }); </script>
The example demonstrates how to effectively use scoped slots in Vue 3 to create a flexible and reusable component structure. The parent component can customize the rendering of each item while still accessing the data provided by the child component.
This example illustrates how you can utilize slots in a Vue 3 application to create a flexible food delivery component system. You can easily customize the header, footer, and content without modifying the main component. If you have any further questions or need more details, feel free to ask in a comment.
By effectively leveraging normal and scoped slots in Vue 3 with the Composition API, you can build highly reusable and customizable UI components, promoting maintainability and code organization in your Vue applications. You can choose the appropriate slot type based on your specific content injection and dynamic rendering requirements.
The way to grow is to connect.
Happy coding!
The above is the detailed content of Understanding Slots in Vue ith Composition API. For more information, please follow other related articles on the PHP Chinese website!