scoped slots and named slots in vuejs

 named slots -> postion of element

scoped slots

in b-table we will have column names

in that column modify or dynamic we use v-slot to access taht column

<b-table>

<template v-slot:cell(actions)="row">

</template>

</b-table>


child table

<template v-slot:row-details="row">

<b-table>


named slots



b-modal directly headers -

<div slot="modal-footer" class="w-100">
<b-button class="mt-3 float-right" @click="cancel('modal-prevent-closing')">Cancel</b-button>
<b-button
class="mt-3 float-right"
variant="primary"
v-on:click="AddPublications()"
style="margin-right:10px;"
>Add</b-button>
</div>


<template v-slot:modal-footer="{cancel}">
<b-button size="sm" variant="secondary" @click="blockAPiCall()">Cancel</b-button>
</template>



The primary difference between named slots and scoped slots is not in how data is passed but in how the content is rendered and accessed.

In named slots, you define specific areas or slots in the child component's template where the parent component can provide custom content. The parent component explicitly specifies which content should be placed in each named slot. This approach is suitable when you have predefined areas in the child component's template that need to be filled with specific content.

Scoped slots, on the other hand, provide more flexibility and control over the rendering and behavior of the child component. With scoped slots, the child component defines a template that exposes data or methods to the parent component. The parent component can then use that data or invoke those methods within its own template to render content dynamically. Scoped slots allow for dynamic content generation and conditional rendering based on the child component's internal state or logic.

So, while both named slots and scoped slots can be used to pass data from the parent component to the child component using props, scoped slots provide more advanced capabilities for dynamic content generation and interaction between the parent and child components. The choice between named slots and scoped slots depends on the specific requirements and complexity of your component composition.






In Vue.js, both slots and scoped slots are features that allow you to pass content from a parent component to a child component. They enable more flexible and dynamic composition of components. However, there are some key differences between slots and scoped slots:

Slots: Slots are a mechanism for content distribution in Vue.js components. They allow you to define placeholders in the component's template where the parent component can provide custom content. Slots are useful when you want to insert content into predefined areas of a component's template.

  1. Named Slots: With named slots, you can define multiple slots in a component and give them names. This allows the parent component to specify which content should be inserted into which slot.

Example: Child Component:

<div>
  <slot name="header"></slot>
  <slot></slot> <!-- Default slot -->
</div>

s

Parent Component:

 s

<child-component>
  <template v-slot:header>
    <h1>Header content</h1>
  </template>
  <p>Main content</p>
</child-component>

In this example, the parent component provides a <h1> element as the content for the named slot "header" and a <p> element as the content for the default slot. The child component's template defines where these slots should be placed.

  1. Scoped Slots: Scoped slots, introduced in Vue.js 2.1, allow the child component to expose data to the parent component. This enables more dynamic and interactive composition of components. The parent component can access the data provided by the child component and use it in its own template.

Example: Child Component:

<div>
  <slot :user="user"></slot>
</div>

Parent Component

<child-component>
  <template v-slot="{ user }">
    <p>Welcome, {{ user.name }}</p>
  </template>
</child-component>

s

In this example, the child component exposes the user object to the parent component using a scoped slot. The parent component receives the user object and can use it in its template to display a personalized message.

Scoped slots provide a way for child components to pass data to the parent component, which allows for greater flexibility in composing components.

In summary, slots are used to pass content from the parent component to predefined areas in the child component's template, while scoped slots enable two-way communication between the parent and child components by allowing the child component to expose data to the parent component.

 s

// Parent component
<template>
  <child-component v-on:message="handleMessage"></child-component>
</template>
<script>
  import ChildComponent from './ChildComponent.vue'
  export default {
    components: { ChildComponent },
    methods: {
      handleMessage(message) {
        console.log(message)
      }
    }
  }
</script>

// Child component
<template>
  <button v-on:click="sendMessage">Send message</button>
</template>
<script>
  export default {
    methods: {
      sendMessage() {
        this.$emit('message', 'Hello from child')
      }
    }
  }
</script>

s


 s

// Parent component
<template>
  <child-component v-on:message="handleMessage"></child-component>
</template>
<script>
  import ChildComponent from './ChildComponent.vue'
  export default {
    components: { ChildComponent },
    methods: {
      handleMessage(message) {
        console.log(message)
      }
    }
  }
</script>

// Child component
<template>
  <button v-on:click="sendMessage">Send message</button>
</template>
<script>
  export default {
    methods: {
      sendMessage() {
        this.$emit('message', 'Hello from child')
      }
    }
  }
</script>

s


No comments:

Post a Comment

Event listening in react

 How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...