Vue.js特性Scoped Slots的淺析
什么是scoped slots
A scoped slot is a special type of slot that functions as a reusable template (that can be passed data to) instead of already-rendered-elements.
上面是官方的定義。
作用域插槽(Scoped Slots)是vue.js中一個(gè)非常有用的特性,它可以使組件更加通用和復(fù)用。唯一的問題是理解起來比較困難。試圖去讓你理解父與子作用域的交織關(guān)系,像解決一道數(shù)學(xué)難題。
簡(jiǎn)單點(diǎn)說slot就是插槽,它是可以被替換掉的,替換它的內(nèi)容是可以拿到當(dāng)前組件的上下文的
(為了簡(jiǎn)單起見,以下例子以模板為主)
舉個(gè)簡(jiǎn)單的例子
//button.vue
<template>
<button class="btn">
<slot></slot> //相當(dāng)于是占位
</button>
</template>
<script>
export default {
}
</script>
//app.vue
<template>
<div id="app">
<Button>Buton with text</Button>
<Button>
<i class="fa fa-copy"></i>//這里取代了slot的位置
</Button>
<Button>
<i class="fa fa-user"></i>Profile
</Button>
</div>
</template>
<script>
import Button from './components/Button.vue'
export default {
name: 'app',
components: {
Button
}
}
</script>
slot其實(shí)就是一個(gè)占位,button.vue的slot位置會(huì)被app.vue里面的替換了。
復(fù)雜例子1:slot內(nèi)的東西可以獲取父組件的上下文信息
//list.vue <template> <div> <slot v-for="item in items" :item="item">//這里是slot的占位 </slot> </div> </template>
//app.vue
<template>
<div id="app">
<List :items="listItems">
<div slot-scope="row"
class="list-item1">//這里可以獲取到item,item原本是屬于List組件內(nèi)部的。也就是說slot獲取了父組件的上下文。
{{row.item.text}}
</div>
</List>
</div>
</template>
解釋見上面代碼注釋。注意一點(diǎn)的是slot-scope=”row” 這里的名字(row)是可以任意取的。
named slots
可以直接放到普通標(biāo)簽上面,可以放template標(biāo)簽上
slot里面的作用域是普通標(biāo)簽或者template是一致的。不能訪問父組件的作用域。
復(fù)雜例子2:slot里面是可以放東西的,是默認(rèn)的模板,可被替換。
//table.vue
<template>
<table class="table">
<thead>
<slot name="columns">//這里定義了一個(gè)slot,名字叫columns,也就是說這里的內(nèi)容是可以被替換掉的
<th v-for="column in columns">
{{column}}
</th>
</slot>
</thead>
<tbody>
<tr v-for="item in data">
<slot :row="item">//這里slot有一個(gè)prop是row
<td v-for="column in columns"
v-if="hasValue(item, column)">
{{itemValue(item, column)}}
</td>
</slot>
</tr>
</tbody>
</table>
</template>
//app.vue
<template>
<div id="app">
<CustomTable :data="tableData"
:columns="tableColumns">
</CustomTable>
<div class="table-separator"></div>
<CustomTable :data="tableData">
<template slot="columns">//這里有一個(gè)slot="columns",意思是替換table.vue里面名字叫columns的slot
<th>Title</th>
<th>
<i class="fa fa-images"></i> Image
</th>
<th class="actions-row">
<i class="fab fa-vuejs vue-icon"></i> Actions
</th>
</template>
<template slot-scope="{row}">//這里替換table.vue里面slot為row的內(nèi)部?jī)?nèi)容
<td class="bold-row">
{{row.title}}
</td>
<td class="img-row">
<img :src="row.img">
</td>
<td class="actions-row">
<Button @click.native="handleAction('Edit')">
<i class="fa fa-edit"></i>
</Button>
<Button @click.native="handleAction('Delete')" type="danger">
<i class="fa fa-trash"></i>
</Button>
</td>
</template>
</CustomTable>
</div>
</template>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue3 emits結(jié)合回調(diào)函數(shù)的使用方式
文章介紹了回調(diào)函數(shù)的概念,并通過一個(gè)簡(jiǎn)單的例子來說明其工作原理,接著,文章提到在Vue.js中,回調(diào)函數(shù)常用于子父組件之間的通信,父組件可以接受子組件的消息并調(diào)用回調(diào)函數(shù),文章通過代碼示例和解釋,幫助讀者理解回調(diào)函數(shù)在實(shí)際開發(fā)中的應(yīng)用2024-12-12
Vue實(shí)現(xiàn)仿iPhone懸浮球的示例代碼
這篇文章主要介紹了Vue實(shí)現(xiàn)仿iPhone懸浮球的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
vue3?使用provide?inject父子組件傳值失敗且子組件不響應(yīng)
這篇文章主要介紹了vue3使用provide?inject父子組件傳值傳不過去且傳遞后子組件不具備響應(yīng)性問題解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
Vue中在data里面調(diào)用method方法的實(shí)現(xiàn)
這篇文章主要介紹了Vue中在data里面調(diào)用method方法的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
vue使用prop可以渲染但是打印臺(tái)報(bào)錯(cuò)的解決方式
今天小編就為大家分享一篇vue使用prop可以渲染但是打印臺(tái)報(bào)錯(cuò)的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
簡(jiǎn)單實(shí)現(xiàn)vue中的依賴收集與響應(yīng)的方法
這篇文章主要介紹了簡(jiǎn)單實(shí)現(xiàn)vue中的依賴收集與響應(yīng)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
前端自動(dòng)化測(cè)試Vue中TDD和單元測(cè)試示例詳解
這篇文章主要為大家介紹了前端自動(dòng)化測(cè)試Vue中TDD和單元測(cè)試示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

