最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue slot插槽作用與原理深入講解

 更新時(shí)間:2023年01月17日 15:31:23   作者:AI3D_WebEngineer  
插槽slot可以說(shuō)在一個(gè)Vue項(xiàng)目里面處處都有它的身影,比如我們使用一些UI組件庫(kù)的時(shí)候,我們通??梢允褂貌宀蹃?lái)自定義我們的內(nèi)容,這篇文章主要介紹了Vue3中slot插槽使用方式,需要的朋友可以參考下

前言

在2.6.0中,具名插槽 和 作用域插槽 引入了一個(gè)新的統(tǒng)一的語(yǔ)法 (即v-slot 指令)。它取代了 slot 和 slot-scope。

什么是Slot

當(dāng)我們生成好組件模板后,為了方便用戶(hù)在調(diào)用組件的時(shí)候自定義組件內(nèi)部分元素的樣式或內(nèi)容。于是在設(shè)計(jì)組件模板的時(shí)候會(huì)挖一個(gè)坑,等待用戶(hù)使用v-slot來(lái)替換坑的slot位置。

栗子

// father.vue
<template>
  <div>
    <child>
      <h1>AAAA</h1>
    </child>
  </div>
</template>
<template>
  <div>
    <p>這里是子組件哦</p>
    <slot></slot>
  </div>
</template>

可以看見(jiàn) <h1>AAAA</h1>被 插入到child的里面。

除了可以插入HTML模板代碼外,你也可以插入普通文本、其他組件、本組件的數(shù)據(jù)。

在插槽中使用數(shù)據(jù)

// father.vue
<template>
  <div>
    <child>
      {{testName}}
      <h1>AAAA</h1>
    </child>
  </div>
</template>
<script>
    ...
    testName = 'Test101';
    ...
</script>

插槽可以使用當(dāng)前引用子組件的組件數(shù)據(jù),但是不能引用傳遞給子組件的數(shù)據(jù)。

// father.vue
<template>
  <div>
   <child :childName="testName">
        {{ childName }}
        <h1>AAAA</h1>
    </child>
  </div>
</template>
<script>
    ...
    testName = 'Test101';
    ...
</script>
// child.vue
<template>
  <div>
    <p>這里是子組件{{ childName }}</p>
    <slot></slot>
  </div>
</template>
<script>
import { Component, Vue, Prop } from 'vue-property-decorator';
@Component()
export default class AAChild extends Vue {
  @Prop() childName;
}
</script>

這里是獲取不到childName的,因?yàn)檫@個(gè)值是傳給<child>的

備胎插槽

// father.vue
<div>
   <child :childName="testName"> </child>
</div>
// child.vue
<div>
    <p>這里是子組件{{ childName }}</p>
    <slot> 我是備胎 </slot>
</div>

給插槽設(shè)置一個(gè)具體的默認(rèn)內(nèi)容,當(dāng)別的組件沒(méi)有給你內(nèi)容的時(shí)候,那么默認(rèn)的內(nèi)容就會(huì)被渲染。如果我們提供內(nèi)容,那么默認(rèn)的插槽內(nèi)容會(huì)被我們的內(nèi)容覆蓋。

具名插槽

當(dāng)我們一個(gè)組件里需要定義多個(gè)插槽時(shí),需要使用slot元素的特性name來(lái)定義額外的插槽。

// child.vue
<template>
  <div>
    <p>插槽一的位置</p>
    <slot name="slot1"> </slot>
    <p>------------------------</p>
    <p>插槽二的位置</p>
    <slot> </slot>
    <p>------------------------</p>
    <p>插槽三的位置</p>
    <slot name="slot3"> </slot>
  </div>
</template>
// father.vue
 <child>
    <template v-slot:slot1>
      <h1>slotOne</h1>
    </template>
    <template>
     <h1>slotTwo</h1> 
    </template>
    <template v-slot:slot3> 
        <h1>slotThree</h1> 
    </template>
</child>

如果一個(gè)<slot>不帶name屬性的話,那么它的name默認(rèn)為default,可以不用v-slot去指定它。(如果你非要折騰,也可以寫(xiě)name="default")

在向具名插槽提供內(nèi)容的時(shí)候,我們可以在<template>元素上使用v-slot指令,并以參數(shù)的形式提供其名稱(chēng)。

注:v-slot只能添加在一個(gè)<template>上,(只有一種例外情況,下面會(huì)說(shuō))

覆蓋問(wèn)題

當(dāng)存在同名的v-slot的時(shí)候,后面會(huì)覆蓋前面的。

當(dāng)存在兩個(gè)匿名的插槽的時(shí)候,兩者都會(huì)被丟進(jìn)去默認(rèn)插槽里。

只更改了father.vue

// father.vue
 <child>
        <template v-slot:slot1>
          <h1>slotOne</h1>
        </template>
        <template> <h1>slotTwo</h1> </template>
        <template v-slot:slot3> <h1>slotThree</h1> </template>
        <template v-slot:slot3> <h1>slotThreeAAAAAAAAA</h1> </template>
        <template> <h1>slotTwoAAAAAAAAAAAA</h1> </template>
</child>

作用域插槽

插槽跟模板其他地方一樣都可以訪問(wèn)當(dāng)前father組件的作用域而不能訪問(wèn)<child>的作用域

如果要訪問(wèn)child的作用域該怎么辦呢?

// child.vue
<template>
  <div>
    <p>下面有一個(gè)插槽</p>
    <slot :aName="name"></slot>
  </div>
</template>
<script>
import { Component, Vue, Prop } from 'vue-property-decorator';
@Component()
export default class AAChild extends Vue {
  name = '123';
}
</script>
// father.vue
<template>
    <div>
     <child>
        <template v-slot:default="slotProps">
          <h1>{{ slotProps.aName }}</h1>
        </template>
      </child>
    </div>
</template>

v-slot:default="slotProps"接住了child組件里在slot元素上綁定的作用域?qū)傩?,同時(shí)吧template只想了默認(rèn)插槽。

具名插槽的作用域

// father.vue
<template>
    <div>
      <child>
        <template v-slot:default="slotProps">
          <h1>{{ slotProps.aName }}</h1>
        </template>
        <template v-slot:slotA="slotProps">
          <h1>{{ slotProps.nameA }}</h1>
        </template>
      </child>
    </div>
</template>
// child.vue
<template>
  <div>
    <p>下面有一個(gè)插槽</p>
    <slot :aName="name"></slot>
    <slot :nameA="nameA" name="slotA"></slot>
  </div>
</template>
<script>
import { Component, Vue, Prop } from 'vue-property-decorator';
@Component()
export default class AAChild extends Vue {
  name = '123';
  nameA = '321';
}
</script>

過(guò)時(shí)的寫(xiě)法:

<template slot="custom" slot-scope="item"></template >

現(xiàn)在的寫(xiě)法:

<template v-slot:custom="{item}"></template >

解構(gòu)插槽Prop

v-slot 的值實(shí)際上可以是任何能夠作為函數(shù)定義中的參數(shù)的 JS 表達(dá)式(作用域插槽 的內(nèi)部工作原理是將你的插槽內(nèi)容包括在一個(gè)傳入單個(gè)參數(shù)的函數(shù)里)

// child.vue
<template>
  <div>
    <p>下面有一個(gè)插槽</p>
    <slot :nameData="nameObj"></slot>
  </div>
</template>
<script>
import { Component, Vue, Prop } from 'vue-property-decorator';
@Component()
export default class AAChild extends Vue {
  nameObj = {
    name: '插槽君',
    key: '202203241567',
  };
}
</script>
// father.vue
<template>
    <div>
      <child>
        <template v-slot="slotProps">
          <h1>{{ slotProps.nameData.name }}</h1>
        </template>
      </child>
    </div>
</template>

father,vue可以改寫(xiě)為

// father.vue
<template>
    <div>
      <child>
        <template v-slot="{ nameData }">
          <h1>{{ nameData.name }}</h1>
        </template>
      </child>
    </div>
</template>

這樣可以使模板更簡(jiǎn)潔。

具名插槽的縮寫(xiě)

跟 v-on 和 v-bind 一樣,v-slot 也有縮寫(xiě),即把參數(shù)之前的所有內(nèi)容 (v-slot:) 替換為字符 #。例如 v-slot:header 可以被重寫(xiě)為 #header:

// father.vue
<template>
    <div>
      <child>
        <template v-slot:keyDDD="{ nameData }">
          <h1>{{ nameData.name }}</h1>
        </template>
      </child>
    </div>
</template>

等同于

// father.vue
<template>
    <div>
      <child>
        <template #keyDDD="{ nameData }">
          <h1>{{ nameData.name }}</h1>
        </template>
      </child>
    </div>
</template>

$scopedSlots

和slot-scope 的區(qū)別?

  • 作用相同:都是作用域插槽
  • 場(chǎng)景不同:slot-scope 是模板語(yǔ)法,scopedSlots 則是編程式語(yǔ)法
  • 使用不同:在 <template> 中使用 slot-scope,在 render() 函數(shù)中使用 scopedSlots
<template v-if="$scopedSlots.label" v-slot:title="{ data }">
     <slot name="label" :data="data" > </slot>
</template>

或者

  render() {
    return this.$scopedSlots.default
      ? this.$scopedSlots.default(this.title)
      : null;
  }

到此這篇關(guān)于Vue slot插槽作用與原理深入講解的文章就介紹到這了,更多相關(guān)Vue slot插槽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue2項(xiàng)目中Mock.js的完整集成與使用教程

    Vue2項(xiàng)目中Mock.js的完整集成與使用教程

    Mock.js 是一個(gè)可以在開(kāi)發(fā)階段模擬后端數(shù)據(jù)接口的 JavaScript 庫(kù),它能夠生成大量不同類(lèi)型的隨機(jī)數(shù)據(jù),并且模擬真實(shí)的接口返回,允許前端開(kāi)發(fā)在沒(méi)有真實(shí)后端接口的情況下進(jìn)行開(kāi)發(fā),本文給大家介紹了Vue2項(xiàng)目中Mock.js的完整集成與使用教程,需要的朋友可以參考下
    2025-02-02
  • 基于iview的router常用控制方式

    基于iview的router常用控制方式

    這篇文章主要介紹了基于iview的router常用控制方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-05-05
  • vue實(shí)現(xiàn)一個(gè)矩形標(biāo)記區(qū)域(rectangle marker)的方法

    vue實(shí)現(xiàn)一個(gè)矩形標(biāo)記區(qū)域(rectangle marker)的方法

    這篇文章主要介紹了vue實(shí)現(xiàn)一個(gè)矩形標(biāo)記區(qū)域 rectangle marker的方法,幫助大家實(shí)現(xiàn)區(qū)域標(biāo)記功能,感興趣的朋友可以了解下
    2020-10-10
  • vue中的計(jì)算屬性實(shí)例詳解

    vue中的計(jì)算屬性實(shí)例詳解

    模板內(nèi)的表達(dá)式非常便利,但是設(shè)計(jì)它們的初衷是用于簡(jiǎn)單運(yùn)算的。接下來(lái)通過(guò)本文給大家介紹vue中的計(jì)算屬性,需要的朋友可以參考下
    2018-09-09
  • 詳解Vue如何監(jiān)測(cè)數(shù)組的變化

    詳解Vue如何監(jiān)測(cè)數(shù)組的變化

    在 Vue 中,如果直接對(duì)數(shù)組進(jìn)行操作,比如使用下標(biāo)直接修改元素,數(shù)組長(zhǎng)度不變時(shí), Vue 是無(wú)法監(jiān)測(cè)到這種變化的,導(dǎo)致無(wú)法觸發(fā)視圖更新。針對(duì)該問(wèn)題本文為大家整理了一些方法,需要的可以參考一下
    2023-01-01
  • vue中axios的封裝問(wèn)題(簡(jiǎn)易版攔截,get,post)

    vue中axios的封裝問(wèn)題(簡(jiǎn)易版攔截,get,post)

    這篇文章主要介紹了vue中axios的封裝問(wèn)題(簡(jiǎn)易版攔截,get,post),需要的朋友可以參考下
    2018-06-06
  • axios post提交formdata的實(shí)例

    axios post提交formdata的實(shí)例

    下面小編就為大家分享一篇axios post提交formdata的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • VUE頁(yè)面中通過(guò)雙擊實(shí)現(xiàn)復(fù)制表格中內(nèi)容的示例代碼

    VUE頁(yè)面中通過(guò)雙擊實(shí)現(xiàn)復(fù)制表格中內(nèi)容的示例代碼

    這篇文章主要介紹了VUE頁(yè)面中通過(guò)雙擊實(shí)現(xiàn)復(fù)制表格中內(nèi)容,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • 不同場(chǎng)景下Vue中虛擬列表實(shí)現(xiàn)

    不同場(chǎng)景下Vue中虛擬列表實(shí)現(xiàn)

    虛擬列表用來(lái)解決大數(shù)據(jù)量數(shù)據(jù)渲染問(wèn)題,由于一次性渲染性能低,所以誕生了虛擬列表渲染,下面我們就來(lái)學(xué)習(xí)一下不同場(chǎng)景下Vue中虛擬列表是如何實(shí)現(xiàn)的吧
    2023-10-10
  • vue中如何使用lodash的debounce防抖函數(shù)

    vue中如何使用lodash的debounce防抖函數(shù)

    防抖函數(shù) debounce 指的是某個(gè)函數(shù)在某段時(shí)間內(nèi),無(wú)論觸發(fā)了多少次回調(diào),都只執(zhí)行最后一次,在Vue中使用防抖函數(shù)可以避免在頻繁觸發(fā)的事件中重復(fù)執(zhí)行操作,這篇文章主要介紹了vue中使用lodash的debounce防抖函數(shù),需要的朋友可以參考下
    2024-01-01

最新評(píng)論

沽源县| 惠州市| 温宿县| 江达县| 东乌珠穆沁旗| 临猗县| 敖汉旗| 霍林郭勒市| 邢台市| 元朗区| 固安县| 太原市| 馆陶县| 天水市| 井陉县| 黔西| 宝鸡市| 伊宁县| 潼关县| 冷水江市| 平原县| 塔城市| 新乡市| 太仆寺旗| 互助| 昌宁县| 都昌县| 南和县| 宝兴县| 嵊泗县| 鹿泉市| 虹口区| 贵港市| 两当县| 南丰县| 锦州市| 周口市| 乃东县| 祁连县| 应用必备| 鄱阳县|