Vue3和Vue2的slot-scope插槽用法解讀
Vue2slot-scope插槽用法
//vue2.x的寫法 <el-table-column label="測(cè)試" 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="測(cè)試" 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)在我們封裝一個(gè)組件,在組件中可以自定義內(nèi)容。
這個(gè)時(shí)候我們就可以使用插槽了。
插槽可以將父頁面中的內(nèi)容展示在子組件中指定的位置。
父頁面
//父頁面 <template> <div> <cha-cao> <template v-slot> 匿名插槽添加的數(shù)據(jù) </template> </cha-cao> </div> </template> <script setup> import ChaCao from "../components/ChaCao.vue" </script>
//子組件 <template> <div> <h2>我是組件中標(biāo)題</h2> <!-- 匿名插槽添加的數(shù)據(jù) 將會(huì)被展示在這里 --> <slot></slot> </div> </template> <!-- 由于組件中只有一個(gè)插槽,我們可以不攜帶參數(shù) -->
說明:
子當(dāng)組件渲染的時(shí)候, 將會(huì)被替換為“匿名插槽添加的數(shù)據(jù) ”。
插槽還可以包含任何模板代碼,包括 HTML,或者其他組件。
第二種插槽(具名插槽)以及插槽簡寫
很多的時(shí)候,我們可能在組件的不同位置展示不同的內(nèi)容。
這個(gè)時(shí)候我們就需要使用具名插槽。
跟 v-on 和 v-bind 一樣,v-slot 也有縮寫。
(v-slot:) 替換為字符 #
例如 v-slot:header 可以被重寫為 #header:
具名插槽的使用
<template> <div> <cha-cao> <template v-slot:header> <h2>標(biāo)題是學(xué)習(xí)vue3</h2> </template> <template v-slot:cont> <h3>正文是好好學(xué)習(xí),天天向上</h3> </template> </cha-cao> </div> </template> <script setup> import ChaCao from "../components/ChaCao.vue" </script>
子組件
<template> <div> <h2>我是組件中標(biāo)題</h2> <slot name="header"></slot> </div> <p>========================</p> <div> <h2>我是正文</h2> <slot name="cont"></slot> </div> </template>
第三種插槽(作用域插槽)
有時(shí)讓插槽內(nèi)容能夠訪問子組件中才有的數(shù)據(jù)是很有用的。
當(dāng)一個(gè)組件被用來渲染一個(gè)項(xiàng)目數(shù)組時(shí),這是一個(gè)常見的情況,
我們希望能夠自定義每個(gè)項(xiàng)目的渲染方式。
作用域插槽的使用
父組件.vue <template> <div> <cha-cao :listArr="arr"> <template v-slot:header="slotProps"> <h1>下面這個(gè)電視劇是自定義的哈</h1> <h1>這就是作用域插槽哈</h1> <h2 clas>電視劇名稱:{{ slotProps.row.name }} 人物:{{slotProps.row.person }} 序號(hào)--{{ slotProps.index }} </h2> </template> </cha-cao> </div> </template> <script setup> import ChaCao from "../components/ChaCao.vue" let arr=[ {name:'且試天下',person:'豐蘭息'}, {name:'請(qǐng)叫我總監(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 }} 序號(hào):{{ 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是動(dòng)態(tài)的對(duì)于寫入插槽來講
具名插槽:具名插槽的name是固定值(靜態(tài)值)
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue?頁面刷新、重置、更新頁面所有數(shù)據(jù)的示例代碼
Vue.js提供了多種方式來實(shí)現(xiàn)頁面刷新、重置和更新頁面所有數(shù)據(jù)的功能,下面通過示例代碼演示vue?頁面刷新、重置、更新頁面所有數(shù)據(jù),感興趣的朋友跟隨小編一起看看吧2024-01-01Vue + better-scroll 實(shí)現(xiàn)移動(dòng)端字母索引導(dǎo)航功能
better-scroll 是一款重點(diǎn)解決移動(dòng)端(已支持 PC)各種滾動(dòng)場(chǎng)景需求的插件。這篇文章主要介紹了Vue + better-scroll 實(shí)現(xiàn)移動(dòng)端字母索引導(dǎo)航功能,需要的朋友可以參考下2018-05-05vue + socket.io實(shí)現(xiàn)一個(gè)簡易聊天室示例代碼
本篇文章主要介紹了vue + socket.io實(shí)現(xiàn)一個(gè)簡易聊天室示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-03-03基于vue2.0實(shí)現(xiàn)仿百度前端分頁效果附實(shí)現(xiàn)代碼
本文通過實(shí)例代碼給大家介紹了基于vue2.0實(shí)現(xiàn)仿百度前端分頁效果,在文中給大家記錄了遇到的問題及解決方法,需要的朋友可以參考下2018-10-10vue3使用xgPalyer實(shí)現(xiàn)截圖功能的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何在vue3中使用xgPalyer截圖功能,以及自定義插件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02