فهرست سرفصل‌های Vue
خانه (HOME) معرفی (Intro) دایرکتیوها (Directives) v-bind (v-bind) v-if (v-if) v-show (v-show) v-for (v-for) رویدادها (Events) v-on (v-on) متدها (Methods) تغییردهنده های رویداد (Event Modifiers) فرم ها (Forms) v-model (v-model) بایندینگ CSS (CSS Binding) ویژگی های محاسبه شده (Computed Properties) واچرها (Watchers) قالب ها (Templates) چرا، چگونه و راه اندازی (Why, How and Setup) اولین صفحه SFC (First SFC Page) کامپوننت ها (Components) پراپس (Props) کامپوننت های v-for (v-for Components) $emit() ($emit()) ویژگی های عبوری (Fallthrough) (Fallthrough Attributes) استایل Scoped (Scoped Styling) کامپوننت های محلی (Local Components) اسلات ها (Slots) v-slot (v-slot) اسلات های Scoped (Scoped Slots) کامپوننت های پویا (Dynamic Components) Teleport (Teleport) درخواست HTTP (HTTP Request) رفرنس های تمپلیت (Template Refs) هوک های چرخه عمر (Lifecycle Hooks) Provide/Inject (Provide/Inject) مسیریابی (Routing) ورودی های فرم (Form Inputs) انیمیشن ها (Animations) انیمیشن با v-for (Animations with v-for) بیلد (Build) Composition API (Composition API) ویژگی های توکار (Built-in Attributes) ویژگی 'is' ('is' Attribute) ویژگی 'key' ('key' Attribute) ویژگی 'ref' ('ref' Attribute) کامپوننت های توکار (Built-in Components) <KeepAlive> (<KeepAlive>) <Teleport> (<Teleport>) <Transition> (<Transition>) <TransitionGroup> (<TransitionGroup>) المان های توکار (Built-in Elements) <component> (<component>) <slot> (<slot>) <template> (<template>) نمونه کامپوننت (Component Instance) $attrs ($attrs) $data ($data) $el ($el) $parent ($parent) $props ($props) $refs ($refs) $root ($root) $slots ($slots) $emit() ($emit()) $forceUpdate() ($forceUpdate()) $nextTick() ($nextTick()) $watch() ($watch()) دایرکتیوها (Directives) v-bind (v-bind) v-cloak (v-cloak) v-for (v-for) v-html (v-html) v-if (v-if) v-else-if (v-else-if) v-else (v-else) v-memo (v-memo) v-model (v-model) v-on (v-on) v-once (v-once) v-pre (v-pre) v-show (v-show) v-slot (v-slot) v-text (v-text) گزینه های نمونه (Instance Options) داده ها (data) متدها (methods) محاسبه شده ها (computed) watch (watch) پراپس (props) emits (emits) expose (expose) هوک های چرخه عمر (Lifecycle Hooks) beforeCreate (beforeCreate) created (created) beforeMount (beforeMount) mounted (mounted) beforeUpdate (beforeUpdate) updated (updated) beforeUnmount (beforeUnmount) unmounted (unmounted) errorCaptured (errorCaptured) renderTracked (renderTracked) renderTriggered (renderTriggered) activated (activated) deactivated (deactivated) serverPrefetch (serverPrefetch) مثال ها (Examples) تمرین ها (Exercises) کوییز (Quiz) سیلابس (Syllabus) برنامه مطالعه (Study Plan) سرور (Server) گواهینامه (Certificate)
نتیجه‌ای برای جستجو یافت نشد.
Vue

Vue — اسلات های Scoped (Scoped Slots)

آخرین بروزرسانی: 1404/08/21

اسلات های Scoped (Scoped Slots)

«اسلات Scoped» یعنی اسلاتی که داده محلی می دهد. «داده محلی» یعنی اطلاعات داخل کامپوننت فرزند. با این روش، والد خودش تصمیم می گیرد چطور آن داده را رندر کند؛ مثل معلمی که جواب را می دهد و تو شکل نوشتن را انتخاب می کنی.

ارسال داده محلی با v-bind

برای فرستادن داده محلی از فرزند، روی تگ <slot> از v-bind استفاده می کنیم.

<!-- SlotComp.vue -->
<template>
  <slot v-bind:lclData="data"></slot>
</template>

<script>
export default {
  data() {
    return {
      data: 'This is local data'
    };
  }
};
<\/script>

مشاهده در ادیتور

دریافت داده در والد با v-slot

والد با v-slot داده اسلات Scoped را می گیرد و هرطور خواست نمایش می دهد.

<!-- App.vue -->
<slot-comp v-slot="dataFromSlot">
  <h2>{{ dataFromSlot.lclData }}</h2>
</slot-comp>

مشاهده در ادیتور

اسلات Scoped با آرایه

می توانیم با v-for چند بار اسلات را صدا بزنیم و هر بار داده متفاوت بفرستیم.

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

مشاهده در ادیتور

آرایه ای از آبجکت ها

می توانیم چند ویژگی بفرستیم؛ سپس در والد هر کدام را رندر کنیم.

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

مشاهده در ادیتور

ارسال داده استاتیک

می توانیم متن ثابت هم بفرستیم. برای استاتیک، v-bind لازم نیست.

<!-- SlotComp.vue -->
<template>
  <slot
    staticText="This text is static"
    :dynamicText="text"
  ></slot>
</template>

<script>
export default {
  data() {
    return {
      text: 'This text is from the data property'
    };
  }
};
<\/script>

مشاهده در ادیتور

<!-- App.vue -->
<slot-comp v-slot="texts">
  <h2>{{ texts.staticText }}</h2>
  <p>{{ texts.dynamicText }}</p>
</slot-comp>

مشاهده در ادیتور

اسلات Scoped نام دار

می توانیم اسلات ها را نام دار کنیم و با v-slot یا # دریافت کنیم.

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

مشاهده در ادیتور

الگوی یک بار ساخت با دو template

می توانیم یک بار کامپوننت را بسازیم و دو template برای اسلات ها بدهیم.

<!-- App.vue -->
<slot-comp>
  <template #leftSlot="leftProps">
    <div>{{ leftProps.text }}</div>
  </template>
  <template #rightSlot="rightProps">
    <div>{{ rightProps.text }}</div>
  </template>
</slot-comp>

مشاهده در ادیتور

گام های عملی سریع

  1. در فرزند، روی <slot> ویژگی های موردنیاز را v-bind کن.
  2. در والد، با v-slot پراپ ها را بگیر و نام بده.
  3. نمایش را هرطور دوست داری طراحی کن.

نکته: برای خوانایی، نام پراپ های اسلات Scoped را کوتاه و معنی دار بگذار.

جمع بندی سریع

  • اسلات Scoped داده محلی را به والد می دهد.
  • ارسال با v-bind روی <slot> انجام می شود.
  • دریافت با v-slot یا # در والد است.
  • می توان چند پراپ، آرایه و آبجکت فرستاد.

اسلات های Scoped با v-slot ترکیب می شوند و انعطاف زیادی می دهند.