Vue.js特性ScopedSlots的示例分析-創(chuàng)新互聯(lián)

這篇文章主要介紹Vue.js特性Scoped Slots的示例分析,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

成都創(chuàng)新互聯(lián)公司專注于陵川網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供陵川營銷型網(wǎng)站建設(shè),陵川網(wǎng)站制作、陵川網(wǎng)頁設(shè)計、陵川網(wǎng)站官網(wǎng)定制、小程序設(shè)計服務(wù),打造陵川網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供陵川網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

什么是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中一個非常有用的特性,它可以使組件更加通用和復(fù)用。唯一的問題是理解起來比較困難。試圖去讓你理解父與子作用域的交織關(guān)系,像解決一道數(shù)學(xué)難題。

簡單點說slot就是插槽,它是可以被替換掉的,替換它的內(nèi)容是可以拿到當(dāng)前組件的上下文的

舉個簡單的例子

//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其實就是一個占位,button.vue的slot位置會被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>

解釋見上面代碼注釋。注意一點的是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">//這里定義了一個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有一個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">//這里有一個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)部內(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>

Vue.js特性Scoped Slots的示例分析

以上是“Vue.js特性Scoped Slots的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計公司行業(yè)資訊頻道!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

本文名稱:Vue.js特性ScopedSlots的示例分析-創(chuàng)新互聯(lián)
地址分享:http://bm7419.com/article24/dsdjce.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號、域名注冊、App設(shè)計、網(wǎng)站策劃定制開發(fā)、面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)