Vue中slot的介紹與使用


vue中的插槽————slot

什么是插槽?

  • 插槽(Slot)是Vue提出來的一個概念,正如名字一樣,插槽用於決定將所攜帶的內容,插入到指定的某個位置,從而使模板分塊,具有模塊化的特質和更大的重用性。
  • 插槽顯不顯示、怎樣顯示是由父組件來控制的,而插槽在哪里顯示就由子組件來進行控制
  • 父組件擁有結構,子組件用slot占位
  • 作用域插槽是一種子傳父傳參的方式,解決了普通slot在parent中無法訪問child數據的去問題;

怎么用插槽?

默認插槽

父組件

<template>
  <div>
    我是父組件
    <slotOne1>
      <p style="color:red">我是父組件插槽內容</p>
    </slotOne1>
  </div>
</template>

 

在父組件引用的子組件中寫入想要顯示的內容(可以使用標簽,也可以不用)

子組件(slotOne1)

<template>
  <div class="slotOne1">
    <div>我是slotOne1組件</div>
    <slot></slot>
  </div>
</template>

 

在子組件中寫入slot,slot所在的位置就是父組件要顯示的內容

  • 當然再父組件引用的子組件中也可以寫入其他組件

父組件

<template>
  <div>
    我是父組件
    <slotOne1>
      <p style="color:red">我是父組件插槽內容</p>
      <slot-one2></slot-one2>
    </slotOne1>
  </div>
</template>

 

子組件(slotOne2)

<template>
  <div class="slotOne2">
    我是slotOne2組件
  </div>
</template>

 

具名插槽

子組件

<template>
  <div class="slottwo">
    <div>slottwo</div>
    <slot name="header"></slot>
    <slot></slot>
    <slot name="footer"></slot>
  </div>
</template>

 

在子組件中定義了三個slot標簽,其中有兩個分別添加了name屬性header和footer

父組件

<template>
  <div>
    我是父組件
    <slot-two>
      <p>啦啦啦,啦啦啦,我是賣報的小行家</p>
      <template slot="header">
          <p>我是name為header的slot</p>
      </template>
      <p slot="footer">我是name為footer的slot</p>
    </slot-two>
  </div>
</template>

 

在父組件中使用template並寫入對應的slot值來指定該內容在子組件中現實的位置(當然也不用必須寫到template),沒有對應值的其他內容會被放到子組件中沒有添加name屬性的slot中

插槽的默認內容

父組件

<template>
  <div>
    我是父組件
    <slot-two></slot-two>
  </div>
</template>

 

子組件

<template>
  <div class="slottwo">
    <slot>我不是賣報的小行家</slot>
  </div>
</template>

 

可以在子組件的slot標簽中寫入內容,當父組件沒有寫入內容時會顯示子組件的默認內容,當父組件寫入內容時,會替換子組件的默認內容

編譯作用域

 

父組件

<template>
  <div>
    我是父組件
    <slot-two>
      <p>{{name}}</p>
    </slot-two>
  </div>
</template>
<script>
export default {
  data () {
    return {
      name: 'Jack'
    }
  }
}
</script>

 

子組件

<template>
  <div class="slottwo">
    <slot></slot>
  </div>
</template>

 

作用域插槽

  • 作用域插槽是一種子傳父傳參的方式,解決了普通slot在parent中無法訪問child數據的去問題;

子組件

<template>
  <div>
    我是作用域插槽的子組件
    <slot :data="user"></slot>
  </div>
</template>

<script>
export default {
  name: 'slotthree',
  data () {
    return {
      user: [
        {name: 'Jack', sex: 'boy'},
        {name: 'Jone', sex: 'girl'},
        {name: 'Tom', sex: 'boy'}
      ]
    }
  }
}
</script>

 

在子組件的slot標簽上綁定需要的值

父組件

<template>
  <div>
    我是作用域插槽
    <slot-three>
      <template slot-scope="user">
        <div v-for="(item, index) in user.data" :key="index">
        {{item}}
        </div>
      </template>
    </slot-three>
  </div>
</template>

 

在父組件上使用slot-scope屬性,user.data就是子組件傳過來的值

 

綜合,利用props屬性傳值,作用域插槽

父組件,

<template>
    <div>
        <p>vue 高級特性</p>
        <hr>

        <ScopedSlotDemo :url="website.url">
            <template slot-scope="slotProps">
                {{slotProps.slotData.title}}
            </template>
        </ScopedSlotDemo>
    </div>
</template>

<script>

import ScopedSlotDemo from './ScopedSlotDemo'
export default {
    data() {
        return {
            name:'小米',
             website: {
                url: 'http://imooc.com/',
                title: 'imooc',
                subTitle: '程序員的夢工廠'
            },
        };
    },
    components:{
        
        ScopedSlotDemo
    }

};
</script>

<style scoped lang="css">

</style>

子組件

<template>
    <a :href="url">
        <slot :slotData="website1">
            {{website1.subTitle}} <!-- 默認值顯示 subTitle ,即父組件不傳內容時 -->
        </slot>
    </a>
</template>

<script>
export default {
    props: ['url'],
    data() {
        return {
            website1: {
                url: 'http://wangEditor.com/',
                title: 'wangEditor',
                subTitle: '輕量級富文本編輯器'
            }
        }
    }
}
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM