Vue2中插槽slot實(shí)現(xiàn)父組件向子組件傳內(nèi)容

插槽slot的作用


通過(guò)插槽slot可以實(shí)現(xiàn)在父組件中傳遞內(nèi)容到子組件的特定位置。譬如在開發(fā)通用組件的時(shí)候溶弟,開放修改通用組件內(nèi)容的地方,讓項(xiàng)目可以實(shí)現(xiàn)個(gè)性化瞭郑,這種情況就可以使用slot插槽技術(shù)辜御。

插槽slot種類


  • 匿名插槽
  • 具名插槽
  • 作用域插槽

插槽slot的基本用法(匿名插槽)


## 子組件
<template>
  <div class="container">
    <el-row >
      <el-col :span="24">
        <slot>
          <span style="color:orange;">子組件默認(rèn)顯示的內(nèi)容</span>
        </slot>
      </el-col>
    </el-row>
  </div>
</template>
    
<script>
  export default {
    name: "CalculateComponent",
    props: {},
    data() {
      return {};
    },
    methods: {},
  };
</script>
<style scoped></style>
 
## 父組件
<template>
    <div class="test">
      <calculate-component>
        <span style="color:red;font-weight:bold;">父組件顯示個(gè)性化內(nèi)容</span>
      </calculate-component>
    </div>
</template>
    
<script>
  import CalculateComponent from '../../components/CalculateComponent.vue'
  export default {
    components: {
      CalculateComponent
    },
    name: "test",
    data() {
      return {
        
      };
    },
    watch: {},
    mounted() {},
    beforeCreate() {},
    created() {},
    methods: {},
  };
</script>
<style scoped></style>

具名插槽(命名插槽)


命名插槽允許你在子組件中定義多個(gè)插槽,并在父組件中指定內(nèi)容應(yīng)傳遞到哪個(gè)插槽屈张。

## 子組件
<template>
  <div class="container">
    <el-row>
      <el-col :span="24">
        <slot name="firstSlot">
          <span style="color:rgb(141, 255, 47);">子組件顯示第一行內(nèi)容</span>
        </slot>
      </el-col>
    </el-row>

    <el-row style="margin-top:30px;">
      <el-col :span="24">
        <slot name="secondSlot">
          <span style="color:chocolate;">子組件顯示第二行內(nèi)容</span>
        </slot>
      </el-col>
    </el-row>
  </div>
</template>
    
<script>
  export default {
    name: "CalculateComponent",
    props: {},
    data() {
      return {};
    },
    methods: {},
  };
</script>
<style scoped></style>

## 父組件
<template>
    <div class="test">
      <calculate-component>
        <template v-slot:firstSlot>
          <span style="color:red;font-weight:bold;">父組件顯示第一行內(nèi)容</span>
        </template>
        <template v-slot:secondSlot>
          <span style="color:blue;font-weight:bold;">父組件顯示第二行內(nèi)容</span>
        </template>
      </calculate-component>
    </div>
</template>
    
<script>
  import CalculateComponent from '../../components/CalculateComponent.vue'
  export default {
    components: {
      CalculateComponent
    },
    name: "test",
    data() {
      return {
        
      };
    },
    watch: {},
    mounted() {},
    beforeCreate() {},
    created() {},
    methods: {},
  };
</script>
<style scoped></style>

作用域插槽


作用域插槽允許你將子組件的數(shù)據(jù)傳遞給父組件的插槽內(nèi)容擒权。在子組件的 <slot> 元素上使用 v-bind 來(lái)傳遞數(shù)據(jù)。

## 子組件
<template>
  <div class="container">
    <el-row>
      <el-col :span="24">
        <slot :userObj="userObj"></slot>
      </el-col>
    </el-row>
  </div>
</template>
    
<script>
  export default {
    name: "CalculateComponent",
    props: {},
    data() {
      return {
        userObj: { name: 'sunpy', age: 31 }
      };
    },
    methods: {},
  };
</script>
<style scoped></style>
   
## 父組件
<template>
    <div class="test">
      <calculate-component>
        <template v-slot:default="slotProps">
          <span style="color:red;font-weight:bold;">
            {{ slotProps.userObj.name }} - {{ slotProps.userObj.age }}
          </span>
        </template>
      </calculate-component>
    </div>
</template>
    
<script>
  import CalculateComponent from '../../components/CalculateComponent.vue'
  export default {
    components: {
      CalculateComponent
    },
    name: "test",
    data() {
      return {
        
      };
    },
    watch: {},
    mounted() {},
    beforeCreate() {},
    created() {},
    methods: {},
  };
</script>
<style scoped></style>

具名插槽和作用域插槽結(jié)合版


插槽的使用更直觀

## 子組件
<template>
  <div class="container">
    <el-row>
      <el-col :span="24">
        <slot 
          name="userObjName"
          :userObj="userObj">
        </slot>
      </el-col>
    </el-row>
  </div>
</template>
    
<script>
  export default {
    name: "CalculateComponent",
    props: {},
    data() {
      return {
        userObj: { name: 'sunpy', age: 31 }
      };
    },
    methods: {},
  };
</script>
<style scoped></style>
    
# 父組件
<template>
    <div class="test">
      <calculate-component>
        <template v-slot:userObjName="slotProps">
          <span style="color:red;font-weight:bold;">
            {{ slotProps.userObj.name }} - {{ slotProps.userObj.age }}
          </span>
        </template>
      </calculate-component>
    </div>
</template>
    
<script>
  import CalculateComponent from '../../components/CalculateComponent.vue'
  export default {
    components: {
      CalculateComponent
    },
    name: "test",
    data() {
      return {
        
      };
    },
    watch: {},
    mounted() {},
    beforeCreate() {},
    created() {},
    methods: {},
  };
</script>
<style scoped></style>

插槽slot修改成熟組件的值


elementui組件修改表格的自定義表頭

  • 說(shuō)明:slot-scope
    slot-scope 是 Vue.js 中用于定義作用域插槽(Scoped Slots)的一個(gè)指令阁谆。作用域插槽允許父組件訪問子組件插槽內(nèi)容中的數(shù)據(jù)碳抄。在 Vue 2.6.0+ 版本中,slot-scope 已經(jīng)被新的 v-slot 指令所替代场绿,但 slot-scope 仍然在許多舊代碼庫(kù)中可見剖效。

  • 例如:

<!-- 子組件 -->
<template>
  <slot :data1="value1" :data2="value2"></slot>
</template>

<script>
export default {
  data() {
    return {
      value1: 'First Value',
      value2: 'Second Value'
    };
  }
};
</script>

<!-- 父組件 -->
<template>
  <child-component>
    <template slot-scope="scope">
      <p>Data 1: {{ scope.data1 }}</p>
      <p>Data 2: {{ scope.data2 }}</p>
    </template>
  </child-component>
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: {
    ChildComponent
  }
};
</script>

注意


  • 插槽的內(nèi)容只會(huì)在父組件中渲染,子組件不能控制插槽內(nèi)容的渲染邏輯焰盗。
  • 使用插槽時(shí)贱鄙,要確保子組件的結(jié)構(gòu)和插槽的使用方式符合預(yù)期,避免出現(xiàn)內(nèi)容渲染不正確的情況姨谷。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末逗宁,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子梦湘,更是在濱河造成了極大的恐慌瞎颗,老刑警劉巖件甥,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異哼拔,居然都是意外死亡引有,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門倦逐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)譬正,“玉大人,你說(shuō)我怎么就攤上這事檬姥≡遥” “怎么了?”我有些...
    開封第一講書人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵健民,是天一觀的道長(zhǎng)抒巢。 經(jīng)常有香客問我,道長(zhǎng)秉犹,這世上最難降的妖魔是什么蛉谜? 我笑而不...
    開封第一講書人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮崇堵,結(jié)果婚禮上型诚,老公的妹妹穿的比我還像新娘。我一直安慰自己鸳劳,他們只是感情好俺驶,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著棍辕,像睡著了一般暮现。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上楚昭,一...
    開封第一講書人閱讀 51,287評(píng)論 1 301
  • 那天栖袋,我揣著相機(jī)與錄音,去河邊找鬼抚太。 笑死塘幅,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的尿贫。 我是一名探鬼主播电媳,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼庆亡!你這毒婦竟也來(lái)了匾乓?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤又谋,失蹤者是張志新(化名)和其女友劉穎拼缝,沒想到半個(gè)月后娱局,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡咧七,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年衰齐,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片继阻。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡耻涛,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出瘟檩,到底是詐尸還是另有隱情抹缕,我是刑警寧澤,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布芒帕,位于F島的核電站歉嗓,受9級(jí)特大地震影響丰介,放射性物質(zhì)發(fā)生泄漏背蟆。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一哮幢、第九天 我趴在偏房一處隱蔽的房頂上張望带膀。 院中可真熱鬧,春花似錦橙垢、人聲如沸垛叨。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)嗽元。三九已至,卻和暖如春喂击,著一層夾襖步出監(jiān)牢的瞬間剂癌,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工翰绊, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留佩谷,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓监嗜,卻偏偏與公主長(zhǎng)得像谐檀,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子裁奇,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容