欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

1、作用

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

2、插槽內(nèi)心

2.1、默認插槽

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

子組件

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

父組件

<template>
  <div class="slotStudy">
    <h1>{{ msg }}</h1>
    <SlotChild>
      <p>這是父組件傳入的值,將會替換子組件中slot中編寫的默認值</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、具名插槽(命名插槽)

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

子組件:

<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>
      <!--      #簡寫-->
      <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ù)傳出,在父組件中使用。子組件渲染作用域插槽時,可以將子組件內(nèi)部的數(shù)據(jù)傳遞給父組件,讓父組件根據(jù)子組件的傳遞過來的數(shù)據(jù)決定如何渲染該插槽。在標(biāo)簽中通過v-slot="要傳過來的數(shù)據(jù)"來接收數(shù)據(jù)。

實現(xiàn)原理

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

子組件:

<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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    Vue?cli及Vue?router實例詳解

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

    vue下載二進制流圖片操作

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

最新評論