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

使用Vue的slot插槽分發(fā)父組件內(nèi)容實現(xiàn)高度復(fù)用、更加靈活的組件(推薦)

 更新時間:2018年05月01日 09:49:59   作者:fozero  
這篇文章主要介紹了使用Vue的slot插槽分發(fā)父組件內(nèi)容實現(xiàn)高度復(fù)用、更加靈活的組件 ,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

寫在前面

之前寫過一篇關(guān)于vue實現(xiàn)dialog會話框組件的文章http://www.fzitv.net/article/139218.htm

 講到了如何實現(xiàn)一個vue對話框組件,其中涉及到了父組件和子組件的通信,這個不用多說,看我之前的文章就能明白,文章最后也說到了,我們可以使用slot插槽來編寫組件,slot用來分發(fā)內(nèi)容到子組件中,從而實現(xiàn)組件的高度復(fù)用,編寫的組件更加靈活。

還是結(jié)合對話框的例子,使用slot來實現(xiàn)對話框組件

注冊一個名叫dialog-tip的全局組件

 Vue.component('dialog-tip', {
   template: '#dialog-tip',
   props:['dialogShow','message'],
   data:function(){
    return {
     content:''
    }
   },
   methods:{
   }
  });

使用templete標簽來定義這個組件

<template id="dialog-tip">
  <div class="dialog_tip" v-if="dialogShow">
   <div class="dialog_tip--mask"></div>
   <div class="dialog_tip--content">
    <div class="dialog_tip--content__txt">
     <slot name="msg">請輸入1-8000之間任意整數(shù)</slot>
    </div>
    <div class="dialog_tip--content__btns">
     <slot>
      <button class="btn">確定</button>
      <button class="btn">重新輸入</button>
      <button class="btn">去注冊</button>
     </slot>
    </div>
   </div>
  </div>
 </template>

<template id="dialog-tip">
  <div class="dialog_tip" v-if="dialogShow">
   <div class="dialog_tip--mask"></div>
   <div class="dialog_tip--content">
    <div class="dialog_tip--content__txt">
     <slot name="msg">請輸入1-8000之間任意整數(shù)</slot>
    </div>
    <div class="dialog_tip--content__btns">
     <slot>
      <button class="btn">確定</button>
      <button class="btn">重新輸入</button>
      <button class="btn">去注冊</button>
     </slot>
    </div>
   </div>
  </div>
 </template>

組件內(nèi)容包括兩部分 ,一個是提示內(nèi)容,一個是button按鈕,我們將要修改替換的內(nèi)容使用slot包含起來,
 這樣父組件就可以分發(fā)內(nèi)容到子組件里面了。

<div class="dialog_tip--content__txt">
     <slot name="msg">請輸入1-8000之間任意整數(shù)</slot>
    </div>
    <div class="dialog_tip--content__btns">
     <slot>
      <button class="btn">確定</button>
      <button class="btn">重新輸入</button>
      <button class="btn">去注冊</button>
     </slot>
    </div>

除了默認插槽,還可以定義具名插槽 ,如果組件中有好幾個部分內(nèi)容需要替換,我們可以為它定義一個name,例如:

<slot name="msg">請輸入1-8000之間任意整數(shù)</slot>

這樣在使用組件的時候,指定slot的name ,就會將這一部分內(nèi)容替換掉,而不會替換其他的插槽內(nèi)容

<p slot="msg">請輸入正確手機號</p>

使用定義好的dialog組件

<dialog-tip message="hello" :dialog-show="dialogShow.tip3">
   <p slot="msg">請輸入正確手機號</p>
   <button class="btn" @click="closeDialogTip('tip3')">確定</button>
  </dialog-tip>
  <dialog-tip message="hello" :dialog-show="dialogShow.tip4">
   <p slot="msg">抱歉,沒有此用戶,請核實后輸入</p>
   <button class="btn" @click="closeDialogTip('tip4')">重新輸入</button>
   <button class="btn" @click="reg">去注冊</button>
  </dialog-tip>

如果不指定slot的名稱,默認dialog-tip標簽里面的內(nèi)容會替換子組件中使用slot包含的內(nèi)容部分,例如以上

使用slot指定了它的名稱來替換子組件中的對應(yīng)的slot部分,而沒有使用slot指定名稱的內(nèi)容會默認將子組件中
 沒有定義具名插槽的部分內(nèi)容替換掉。

需要注意的是,如果dialog-tip標簽里沒有定義需要分發(fā)的內(nèi)容,那么子組件中會顯示默認的插槽內(nèi)容

關(guān)于更多的slot用法,請移步https://cn.vuejs.org/v2/guide/components-slots.html

最后

效果圖

總結(jié)

以上所述是小編給大家介紹的使用Vue的slot插槽分發(fā)父組件內(nèi)容實現(xiàn)高度復(fù)用、更加靈活的組件,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!

相關(guān)文章

最新評論

嵩明县| 仙居县| 武隆县| 安塞县| 林口县| 根河市| 白水县| 太保市| 杂多县| 措美县| 长兴县| 通道| 斗六市| 且末县| 南溪县| 砀山县| 定兴县| 平山县| 双江| 芷江| 威信县| 年辖:市辖区| 兴和县| 宜宾市| 鹤岗市| 华蓥市| 舒兰市| 涪陵区| 达尔| 南溪县| 凤城市| 陆川县| 鄯善县| 黄山市| 利川市| 静宁县| 公安县| 洛川县| 天镇县| 深圳市| 丹巴县|