亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Vue中slot插槽作用與原理詳解

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

1、作用

  • 父組件向子組件傳遞內(nèi)容
  • 擴(kuò)展、復(fù)用、定制組件

2、插槽內(nèi)心

2.1、默認(rèn)插槽

把父組件中的數(shù)組,顯示在子組件中,子組件通過(guò)一個(gè)slot插槽標(biāo)簽顯示父組件中的數(shù)據(jù)。

子組件

<template>
  <div class="slotChild">
    <h4>{{msg}}</h4>
    <slot>這是子組件插槽默認(rèn)的值</slot>
  </div>
</template>
<script>
export default {
  name: "slotChild",
  data() {
    return {
      msg: "vue中的插槽---子組件"
    }
  }
}
</script>

父組件

<template>
  <div class="slotStudy">
    <h1>{{ msg }}</h1>
    <SlotChild>
      <p>這是父組件傳入的值,將會(huì)替換子組件中slot中編寫(xiě)的默認(rèn)值</p>
    </SlotChild>
  </div>
</template>
<script>
import SlotChild from "@/components/slot/SlotChild";
export default {
  name: "slotStudy",
  components: {SlotChild},
  data() {
    return {
      msg: "vue中的插槽---父組件"
    }
  }
}
</script>

    <SlotChild></SlotChild>

2.2、具名插槽(命名插槽)

父組件中通過(guò)slot屬性,給插槽命名,在子組件中通過(guò)slot標(biāo)簽,根據(jù)定義好的名字填充到對(duì)應(yīng)的位置。這樣就可以指定多個(gè)可區(qū)分的slot,在使用組件時(shí)靈活的進(jìn)行插值。

子組件:

<template>
  <div class="slotChild">
    <h4>{{msg}}</h4>
    <h1><slot name="h_1"></slot></h1>
    <h2><slot name="h_2"></slot></h2>
    <h3><slot name="h_3"></slot></h3>
  </div>
</template>
<script>
export default {
  name: "slotChild",
  data() {
    return {
      msg: "vue中的插槽---子組件"
    }
  }
}
</script>

父組件:

<template>
  <div class="slotStudy">
    <h1>{{ msg }}</h1>
    <SlotChild>
      <template v-slot:h_1>h1111111111的內(nèi)容</template>
      <!--      #簡(jiǎn)寫(xiě)-->
      <template #h_2>h2222222的內(nèi)容</template>
      <template v-slot:h_3>h33333的內(nèi)容</template>
    </SlotChild>
  </div>
</template>
<script>
import SlotChild from "@/components/slot/SlotChild";
export default {
  name: "slotStudy",
  components: {SlotChild},
  data() {
    return {
      msg: "vue中的插槽---父組件"
    }
  }
}
</script>

2.3、作用域插槽

用得不多。

將子組件中data的數(shù)據(jù)傳出,在父組件中使用。子組件渲染作用域插槽時(shí),可以將子組件內(nèi)部的數(shù)據(jù)傳遞給父組件,讓父組件根據(jù)子組件的傳遞過(guò)來(lái)的數(shù)據(jù)決定如何渲染該插槽。在標(biāo)簽中通過(guò)v-slot="要傳過(guò)來(lái)的數(shù)據(jù)"來(lái)接收數(shù)據(jù)。

實(shí)現(xiàn)原理

實(shí)現(xiàn)原理:當(dāng)子組件vm實(shí)例化時(shí),獲取到父組件傳入的slot標(biāo)簽的內(nèi)容,存放在vm. s l o t 中,默認(rèn)插槽為 v m . slot中,默認(rèn)插槽為vm. slot中,默認(rèn)插槽為vm.slot.default,具名插槽為vm. s l o t . x x x , x x x 為插槽名,當(dāng)組件執(zhí)行渲染函數(shù)時(shí)候,遇到 s l o t 標(biāo)簽,使用 slot.xxx,xxx 為插槽名,當(dāng)組件執(zhí)行渲染函數(shù)時(shí)候,遇到slot標(biāo)簽,使用 slot.xxx,xxx為插槽名,當(dāng)組件執(zhí)行渲染函數(shù)時(shí)候,遇到slot標(biāo)簽,使用slot中的內(nèi)容進(jìn)行替換,此時(shí)可以為插槽傳遞數(shù)據(jù),若存在數(shù)據(jù),則可稱(chēng)該插槽為作用域插槽。

子組件:

<template>
  <div class="slotChild">
    <h4>{{ msg }}</h4>
    <h1>
      <slot :str="strDate" name="n_str">{{ strDate.name }}</slot>
    </h1>
    <h2>
      <slot :str="strDate" name="j_str">{{ strDate.job }}</slot>
    </h2>
  </div>
</template>
<script>
export default {
  name: "slotChild",
  data() {
    return {
      msg: "vue中的插槽---子組件",
      strDate: {
        name: "學(xué)習(xí)前端的小方同學(xué)",
        job: "找工作中",
        age:"我每年都是18"
      }
    }
  }
}
</script>

父組件:

<template>
  <div class="slotStudy">
    <h1>{{ msg }}</h1>
    <SlotChild>
      <template #n_str="strProps">
        {{ strProps.str.job }}
      </template>
      <template v-slot:j_str="strProps">
        {{ strProps.str.age }}
      </template>
    </SlotChild>
  </div>
</template>
<script>
import SlotChild from "@/components/slot/SlotChild";
export default {
  name: "slotStudy",
  components: {SlotChild},
  data() {
    return {
      msg: "vue中的插槽---父組件"
    }
  }
}
</script>

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

相關(guān)文章

  • 一文搞懂Vue中computed和watch的區(qū)別

    一文搞懂Vue中computed和watch的區(qū)別

    這篇文章主要和大家詳細(xì)介紹一下Vue中computed和watch的使用與區(qū)別,文中通過(guò)示例為大家進(jìn)行了詳細(xì)講解,對(duì)Vue感興趣的同學(xué),可以學(xué)習(xí)一下
    2022-11-11
  • vue項(xiàng)目環(huán)境搭建?啟動(dòng)?移植操作示例及目錄結(jié)構(gòu)分析

    vue項(xiàng)目環(huán)境搭建?啟動(dòng)?移植操作示例及目錄結(jié)構(gòu)分析

    這篇文章主要介紹了vue項(xiàng)目環(huán)境搭建、啟動(dòng)、項(xiàng)目移植、項(xiàng)目目錄結(jié)構(gòu)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-04-04
  • Vue3+Hooks實(shí)現(xiàn)4位隨機(jī)數(shù)和60秒倒計(jì)時(shí)的示例代碼

    Vue3+Hooks實(shí)現(xiàn)4位隨機(jī)數(shù)和60秒倒計(jì)時(shí)的示例代碼

    Vue3的Hooks是一種新的 API,本文主要介紹了Vue3+Hooks實(shí)現(xiàn)4位隨機(jī)數(shù)和60秒倒計(jì)時(shí)的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-04-04
  • vue3路由配置以及路由跳轉(zhuǎn)傳參詳解

    vue3路由配置以及路由跳轉(zhuǎn)傳參詳解

    路由跳轉(zhuǎn)的同時(shí)傳遞參數(shù)是比較常見(jiàn)的,下面這篇文章主要給大家介紹了關(guān)于vue3路由配置以及路由跳轉(zhuǎn)傳參的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-04-04
  • 應(yīng)用provide與inject刷新Vue頁(yè)面方法

    應(yīng)用provide與inject刷新Vue頁(yè)面方法

    這篇文章主要介紹了應(yīng)用provide與inject刷新Vue頁(yè)面的兩種方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,多多進(jìn)步,祝大家早日升職加薪
    2021-09-09
  • reactive readonly嵌套對(duì)象轉(zhuǎn)換功能實(shí)現(xiàn)詳解

    reactive readonly嵌套對(duì)象轉(zhuǎn)換功能實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了reactive readonly嵌套對(duì)象轉(zhuǎn)換功能實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12
  • Vue中mapMutations傳遞參數(shù)方式

    Vue中mapMutations傳遞參數(shù)方式

    這篇文章主要介紹了Vue中mapMutations傳遞參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue3實(shí)戰(zhàn)教程之a(chǎn)xios的封裝和環(huán)境變量

    vue3實(shí)戰(zhàn)教程之a(chǎn)xios的封裝和環(huán)境變量

    這篇文章主要給大家介紹了關(guān)于vue3實(shí)戰(zhàn)教程之a(chǎn)xios的封裝和環(huán)境變量的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-02-02
  • Vue?cli及Vue?router實(shí)例詳解

    Vue?cli及Vue?router實(shí)例詳解

    vue-cli是vue官方出品的快速構(gòu)建單頁(yè)應(yīng)用的腳手架,里面集成了webpack,npm,nodejs,babel,vue,vue-router,這篇文章主要介紹了Vue?cli及Vue?router詳解,需要的朋友可以參考下
    2022-08-08
  • vue下載二進(jìn)制流圖片操作

    vue下載二進(jìn)制流圖片操作

    這篇文章主要介紹了vue下載二進(jìn)制流圖片操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10

最新評(píng)論