Vue3和Vue2的slot-scope插槽用法解讀
Vue2slot-scope插槽用法
//vue2.x的寫法
<el-table-column label="測試" align="center" prop="ce">
<template slot-scope="scope"> //2.x的寫法
<span>{{scope.row.ce}}</span>
</template>
</el-table-column>Vue3slot-scope插槽用法
<el-table-column label="測試" align="center" prop="ce">
<template #default="{row,$index}"> //3.x的新寫法 -- #default="scope" $index
<span>{{row.ce}}</span>
</template>
</el-table-column>看完覺得沒了?恭喜你沒走開,下面的更精彩,
分享Vue3里面v-solt插槽的四種用法
第一種插槽(匿名插槽)
現(xiàn)在我們封裝一個組件,在組件中可以自定義內容。
這個時候我們就可以使用插槽了。
插槽可以將父頁面中的內容展示在子組件中指定的位置。
父頁面
//父頁面
<template>
<div>
<cha-cao>
<template v-slot>
匿名插槽添加的數(shù)據
</template>
</cha-cao>
</div>
</template>
<script setup>
import ChaCao from "../components/ChaCao.vue"
</script>//子組件
<template>
<div>
<h2>我是組件中標題</h2>
<!-- 匿名插槽添加的數(shù)據 將會被展示在這里 -->
<slot></slot>
</div>
</template>
<!-- 由于組件中只有一個插槽,我們可以不攜帶參數(shù) -->
說明:
子當組件渲染的時候, 將會被替換為“匿名插槽添加的數(shù)據 ”。
插槽還可以包含任何模板代碼,包括 HTML,或者其他組件。
第二種插槽(具名插槽)以及插槽簡寫
很多的時候,我們可能在組件的不同位置展示不同的內容。
這個時候我們就需要使用具名插槽。
跟 v-on 和 v-bind 一樣,v-slot 也有縮寫。
(v-slot:) 替換為字符 #
例如 v-slot:header 可以被重寫為 #header:
具名插槽的使用
<template>
<div>
<cha-cao>
<template v-slot:header>
<h2>標題是學習vue3</h2>
</template>
<template v-slot:cont>
<h3>正文是好好學習,天天向上</h3>
</template>
</cha-cao>
</div>
</template>
<script setup>
import ChaCao from "../components/ChaCao.vue"
</script>子組件
<template>
<div>
<h2>我是組件中標題</h2>
<slot name="header"></slot>
</div>
<p>========================</p>
<div>
<h2>我是正文</h2>
<slot name="cont"></slot>
</div>
</template>
第三種插槽(作用域插槽)
有時讓插槽內容能夠訪問子組件中才有的數(shù)據是很有用的。
當一個組件被用來渲染一個項目數(shù)組時,這是一個常見的情況,
我們希望能夠自定義每個項目的渲染方式。
作用域插槽的使用
父組件.vue
<template>
<div>
<cha-cao :listArr="arr">
<template v-slot:header="slotProps">
<h1>下面這個電視劇是自定義的哈</h1>
<h1>這就是作用域插槽哈</h1>
<h2 clas>電視劇名稱:{{ slotProps.row.name }} 人物:{{slotProps.row.person }} 序號--{{ slotProps.index }} </h2>
</template>
</cha-cao>
</div>
</template>
<script setup>
import ChaCao from "../components/ChaCao.vue"
let arr=[
{name:'且試天下',person:'豐蘭息'},
{name:'請叫我總監(jiān)',person:'小橘子'},
{name:'你是我的榮耀',person:'路人甲',slotFlag:true},
]
</script>//子組件
<template>
<ul>
<li v-for="( item, index ) in listArr" :key="index">
<template v-if="!item.slotFlag">
<h2>電視劇名稱:{{ item.name }} 人物:{{item.person }} 序號:{{ index }} </h2>
</template>
<template v-else>
<slot :row="item" name="header" :index="index"></slot>
</template>
</li>
</ul>
</template>
<script setup>
import {defineProps} from 'vue'
defineProps({
listArr:{
type:Array,
default:()=>{
return []
}
},
})
</script>效果:

第四種插槽-寫入插槽
//父頁面
<template>
<div class="main">
{{ name }}==
<cha-cao>
<template #[name]>
<div>我在哪里</div>
</template>
</cha-cao>
</div>
</template>
<script setup lang="ts">
import { ref, } from 'vue'
const name = ref('header')
</script>//子組件
<template>
<div>
<div class="header">
<slot name="header">我是頭部</slot>
</div>
<div class="main">
<slot name="main">我是主體</slot>
</div>
</div>
</template>


寫入插槽與具名插槽的區(qū)別?
最大的區(qū)別是name是動態(tài)的對于寫入插槽來講
具名插槽:具名插槽的name是固定值(靜態(tài)值)
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue3 el-pagination 將組件中英文‘goto’ 修改 為&nbs
這篇文章主要介紹了vue3 el-pagination 將組件中英文‘goto’ 修改 為 中文到‘第幾’,通過實例代碼介紹了vue3項目之Pagination 組件,感興趣的朋友跟隨小編一起看看吧2024-02-02
vue?elementui動態(tài)添加el-input實例代碼
最近遇到一個新的需求,需要動態(tài)添加el-input,這篇文章主要給大家介紹了關于vue?elementui動態(tài)添加el-input的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-06-06
Vue3+Element Plus實現(xiàn)自定義彈窗組件的全屏功能
在現(xiàn)代化的前端開發(fā)中,彈窗組件是提升用戶體驗的重要元素,本文將介紹如何使用 Vue 3 和 Element Plus 庫來創(chuàng)建一個具有全屏功能的自定義彈窗組件,文中通過代碼示例講解的非常詳細,需要的朋友可以參考下2024-07-07
vue-axios同時請求多個接口 等所有接口全部加載完成再處理操作
這篇文章主要介紹了vue-axios同時請求多個接口 等所有接口全部加載完成再處理操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11

