<slot>


Misol

Ota komponentdan kelgan kontentni bola komponentning <template> qismiga joylashtirish uchun o‘rnatilgan <slot> elementidan foydalanish.

<template>
  <div>  
    <p>SlotComp.vue</p>
    <slot></slot>
  </div>
</template>
Misolni ishga tushirish »

Quyida ko‘proq misollarni ko‘ring.


ULASHISH

Ta’rif va qo‘llanilishi

O‘rnatilgan <slot> elementi ota komponentdan olingan kontentni joylashtirish uchun ishlatiladi.

Bola komponent chaqirilganda, uning ochuvchi va yopuvchi teglari orasida berilgan kontent o‘sha bola komponent ichida <slot> elementi turgan joyga tushadi.

Komponentda bir nechta <slot> bo‘lishi mumkin va slotlarga name prop’i yordamida nom berish mumkin. Turli nomli slotlarga ega bunday komponentlarda kontentni muayyan slotlarga yuborish uchun v-slot direktivasidan foydalanishimiz mumkin. (3-misol)

Agar ota komponent hech qanday kontent bermasa, <slot> elementining ochuvchi va yopuvchi teglari orasidagi kontent zaxira (fallback) kontent sifatida ishlatiladi. (5-misol)

Ma’lumotni <slot> props orqali yuqoriga, ota elementga uzatish mumkin. (8-misol)

<slot> elementi kontent uchun shunchaki joy egasi (placeholder), <slot> elementining o‘zi DOM elementi sifatida render qilinmaydi.


Props

Prop Tavsif
[har qanday] Slotlarda aniqlangan props "scoped slot"larni hosil qiladi va bunday props ota komponentga yuboriladi. Misolni ishga tushirish »
name Ota komponent v-slot direktivasi yordamida kontentni muayyan slotga yo‘naltira olishi uchun slotga nom beradi. Misolni ishga tushirish »

Ko‘proq misollar

1-misol

Kartaga o‘xshash ko‘rinish hosil qilish maqsadida dinamik HTML kontentining katta qismlarini o‘rab olish uchun slotlardan foydalanish.

App.vue:

<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>

Kontent <slot> joylashgan komponentga kirar ekan, biz <slot> atrofida div ishlatamiz va <div>’ni lokal uslublaymiz — shu tariqa ilovamizdagi boshqa div’larga ta’sir qilmasdan kontent atrofida kartaga o‘xshash ko‘rinish hosil qilamiz.

SlotComp.vue:

<template>
  <div> <!-- This div makes the card-like appearance -->
    <slot></slot>
  </div>
</template>

<script></script>

<style scoped>
  div {
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
    border-radius: 10px;
    margin: 10px;
  }
</style>
Misolni ishga tushirish »

2-misol

Footer (sahifaning pastki qismi) yaratish uchun slotdan foydalanish.

App.vue:

<template>
  <h3>Reusable Slot Cards</h3>
  <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>
      <h4>Footer</h4>
    </slot-comp>
  </footer>
</template>
Misolni ishga tushirish »

3-misol

Slot nomlaridan foydalanib, kontentni muayyan slotga yuborish mumkin.

SlotComp.vue:

<h3>Component</h3>
<div>
  <slot name="topSlot"></slot>
</div>
<div>
  <slot name="bottomSlot"></slot>
</div>

App.vue:

<h1>App.vue</h1>
<p>The component has two div tags with one slot in each.</p>
<slot-comp v-slot:bottomSlot>'Hello!'</slot-comp>
Misolni ishga tushirish »

4-misol

Komponentda ikkita slot bo‘lsa, komponentga yuborilgan kontent ikkala slotga ham tushadi.

App.vue:

<h1>App.vue</h1>
<p>The component has two div tags with one slot in each.</p>
<slot-comp>'Hello!'</slot-comp>

SlotComp.vue:

<h3>Component</h3>
<div>
  <slot></slot>
</div>
<div>
  <slot></slot>
</div>
Misolni ishga tushirish »

5-misol

Ota komponent hech qanday kontent bermaganda ham nimadir render qilinishi uchun slotda zaxira (fallback) kontentdan foydalanish.

App.vue:

<template>
  <h3>Slots Fallback Content</h3>
  <p>A component without content provided can have fallback content in the slot tag.</p>
  <slot-comp>
    <!-- Empty -->
  </slot-comp>
  <slot-comp>
    <h4>This content is provided from App.vue</h4>
  </slot-comp>
</template>

SlotComp.vue:

<template>
  <div>
    <slot>
      <h4>This is fallback content</h4>
    </slot>
  </div>
</template>
Misolni ishga tushirish »

6-misol

Nomsiz slot ota komponentdan keladigan kontent uchun standart (default) slot bo‘ladi.

SlotComp.vue:

<h3>Component</h3>
<div>
  <slot></slot>
</div>
<div>
  <slot name="bottomSlot"></slot>
</div>

App.vue:

<h1>App.vue</h1>
<p>The component has two div tags with one slot in each.</p>
<slot-comp>'Hello!'</slot-comp>
Misolni ishga tushirish »

7-misol

Slotni aniq qilib standart (default) slot sifatida belgilash uchun v-slot:default’dan foydalanish.

SlotComp.vue:

<h3>Component</h3>
<div>
  <slot></slot>
</div>
<div>
  <slot name="bottomSlot"></slot>
</div>

App.vue:

<h1>App.vue</h1>
<p>The component has two div tags with one slot in each.</p>
<slot-comp v-slot:default>'Default slot'</slot-comp>
Misolni ishga tushirish »

8-misol

Scoped slot: taom nomini ota komponentga uzatish uchun slotdagi "foodName" prop’idan foydalanish.

SlotComp.vue:

<template>
  <slot
    v-for="x in foods"
    :key="x"
    :foodName="x"
  ></slot>
</template>

<script>
  export default {
    data() {
      return {
        foods: ['Apple','Pizza','Rice','Fish','Cake']
      }
    }
  }
</script>

App.vue:

<slot-comp v-slot="food">
  <h2>{{ food.foodName }}</h2>
</slot-comp>
Misolni ishga tushirish »

9-misol

Scoped slot: obyektlardan iborat massiv asosida ota komponentga bir nechta narsani uzatish uchun slotda props’dan foydalanish.

SlotComp.vue:

<template>
  <slot
    v-for="x in foods"
    :key="x.name"
    :foodName="x.name"
    :foodDesc="x.desc"
    :foodUrl="x.url"
  ></slot>
</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 tastes good but is not considered healthy.', url: 'img_cake.svg' }
       ]
      }
    }
  }
</script>

App.vue:

<slot-comp v-slot="food">
  <hr>
  <h2>{{ food.foodName }}<img :src=food.foodUrl></h2>
  <p>{{ food.foodDesc }}</p>
</slot-comp>
Misolni ishga tushirish »

10-misol

Bitta matnni "leftSlot"ga, boshqasini esa "rightSlot"ga joylashtirish uchun nomlangan scoped slotlardan foydalanish.

SlotComp.vue:

<template>
  <slot
    name="leftSlot"
    :text="leftText"
  ></slot>
  <slot
    name="rightSlot"
    :text="rightText"
  ></slot>
</template>

<script>
  export default {
    data() {
      return {
        leftText: 'This text belongs to the LEFT slot.',
        rightText: 'This text belongs to the RIGHT slot.'
      }
    }
  }
</script>

App.vue:

<slot-comp #leftSlot="leftProps">
  <div>{{ leftProps.text }}</div>
</slot-comp>
<slot-comp #rightSlot="rightProps">
  <div>{{ rightProps.text }}</div>
</slot-comp>
Misolni ishga tushirish »


Tegishli sahifalar

Vue darsligi: Vue slotlar

Vue darsligi: Vue v-slot

Vue darsligi: Scoped slotlar

Vue darsligi: Komponentlar

Vue ma’lumotnomasi: Vue v-slot direktivasi

Vue ma’lumotnomasi: Vue $slots obyekti


W3Schools Pathfinder

Yutuqlaringizni kuzating – bu bepul!