vue組件化中slot的基本使用方法
前言
slot可以在子組件中開啟插槽,在父組件引用該組建時(shí),可以定義這個(gè)插槽內(nèi)要展現(xiàn)的功能或模塊,下面話不多說了,來一起看看詳細(xì)的介紹吧
1.單個(gè)slot
子組件中在相應(yīng)位置寫slot標(biāo)簽,父組件在引用子組件時(shí),在子組件標(biāo)簽內(nèi)寫要插入插槽的元素;
還可以設(shè)置slot在父組件沒有設(shè)置插槽時(shí),子組件的插槽默認(rèn)顯示內(nèi)容;
父組件.vue
<template> <div class="home"> <child-componment> <p> 這是父組件的slot替代內(nèi)容! </p> </child-componment> </div> </template> <script> import childComponment from '@/components/childComponment.vue' export default { name: "home", components:{ childComponment }, data(){ return { message: '' } } }; </script>
子組件childComponment.vue
<template> <div class="childComponment"> <h2>這是子組件childComponment!</h2> <slot> <span style="color: red;">如果父組件沒有插入內(nèi)容,我這樣可以設(shè)置默認(rèn)的顯示內(nèi)容</span> </slot> </div> </template> <script> export default { name: "childComponment", data(){ return { message: '' } } }; </script>
2.具名slot(同時(shí)使用多個(gè)插槽)
給slot指定一個(gè)名稱,可以分發(fā)多個(gè)slot插槽,但是只能有一個(gè)無名slot;
父組件的slot插槽內(nèi)容,不寫slot="xxx"的都會(huì)插到子組件的無名slot中;
如果沒有指定無名slot(默認(rèn)slot),父組件內(nèi)多余的內(nèi)容將會(huì)被拋棄。
<template> <div class="home"> <child-componment> <h1 slot="header"> 父組件的header </h1> <h6 slot="footer">父組件的footer</h6> <h6>父組件的無名slot-1</h6> <p> 父組件的無名slot-2 </p> </child-componment> </div> </template> <script> import childComponment from '@/components/childComponment.vue' export default { name: "home", components:{ childComponment }, data(){ return { message: '' } } }; </script>
子組件
<template> <div class="childComponment"> <span>這是子組件childComponment的正常內(nèi)容!</span> <div class="header"> <slot name="header"> <span style="color: red;">子組件默認(rèn)header-slot</span> </slot> </div> <div class="container"> <!-- 如果沒有指定無名slot(默認(rèn)slot),父組件內(nèi)多余的內(nèi)容將會(huì)被拋棄 --> <slot> <span style="color: red;">子組件默認(rèn)無名slot</span> </slot> </div> <div class="footer"> <slot name="footer"> <span style="color: red;">子組件默認(rèn)footer-slot</span> </slot> </div> </div> </template> <script> export default { name: "childComponment", data(){ return { message: '' } } }; </script> <style scoped> .childComponment{ font-size: 16px; } .header{ height: 100px; border:1px solid red; color: red; } .container{ height: 500px; border: 1px solid black; color: black; } .footer{ height:100px; border: 1px grey solid; color: grey; } </style>
3.作用域插槽
<template> <div class="home"> <child-componment> <template slot-scope="slotProps"> <!-- 這里顯示子組件傳來的數(shù)據(jù) --> <p>{{slotProps}}</p> </template> </child-componment> </div> </template> <script> import childComponment from '@/components/childComponment.vue' export default { name: "home", components:{ childComponment } }; </script>
子組件
<template> <div class="childComponment"> <span>這是子組件childComponment的正常內(nèi)容!</span> <div class="container"> <!-- 如果沒有指定無名slot(默認(rèn)slot),父組件內(nèi)多余的內(nèi)容將會(huì)被拋棄 --> <slot msg="子組件信息" slotData="子組件數(shù)據(jù)"></slot> </div> </div> </template>
Tips:
作用于插槽也可是具名插槽
案例:列表組件
這是作用于插槽使用最多的案例,允許組件自定義應(yīng)該如何渲染組件的每一項(xiàng)。
<template> <div class="about"> <h1>This is about page</h1> <my-list :books="books"> <template slot="bookList" slot-scope="myListProps"> <li>{{myListProps.bookName}}</li> </template> </my-list> </div> </template> <script> import myList from '@/components/myList.vue' export default { components:{ myList }, data(){ return { books: [ {name: 'css揭秘'}, {name: '深入淺出nodejs'}, {name: 'javascript設(shè)計(jì)模式與開發(fā)實(shí)戰(zhàn)'} ] } } } </script>
子組件myList.vue
<template> <div class="myList"> <h1>This is myList page</h1> <ul> <slot name="bookList" v-for="book in books" :bookName="book.name"></slot> </ul> </div> </template> <script> export default { props:{ books:{ type: Array, default: function(){ return [] } } }, mounted(){ console.log(this.books) } } </script>
其實(shí)上面的案例可直接在父組件中for循環(huán)就好,此處只是作為演示slot的作用域插槽;
實(shí)際開發(fā)中作用域插槽的使用場(chǎng)景主要為:既可以復(fù)用子組件的slot,又可以使slot內(nèi)容不一致。
4.訪問slot
vue2.0提供了$slot方法來訪問slot
此處代碼以**“具名slot(同時(shí)使用多個(gè)插槽)”**的代碼為例,修改一下子組件childComponment.vue
export default { name: "childComponment", data(){ return { message: '' } }, mounted(){ let header = this.$slots.header let main = this.$slots.default let footer = this.$slots.footer console.log(header) console.log(main) console.log(footer) console.log(footer[0].elm.innerHTML) } };
打印結(jié)果:
其中elm的內(nèi)容為插槽內(nèi)容,結(jié)構(gòu)如下:
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Vue項(xiàng)目保持element組件同行,設(shè)置組件不自動(dòng)換行問題
這篇文章主要介紹了Vue項(xiàng)目保持element組件同行,設(shè)置組件不自動(dòng)換行問題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Vue2(三)實(shí)現(xiàn)子菜單展開收縮,帶動(dòng)畫效果實(shí)現(xiàn)方法
這篇文章主要介紹了vue實(shí)現(xiàn)收縮展開效果的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04使用vue2實(shí)現(xiàn)帶地區(qū)編號(hào)和名稱的省市縣三級(jí)聯(lián)動(dòng)效果
我們知道省市區(qū)縣都有名稱和對(duì)應(yīng)的數(shù)字唯一編號(hào),使用編號(hào)可以更方便查詢以及程序處理,我們今天來了解一下使用vue2來實(shí)現(xiàn)常見的省市區(qū)下拉聯(lián)動(dòng)選擇效果,需要的朋友可以參考下2018-11-11Nuxt.js實(shí)現(xiàn)一個(gè)SSR的前端博客的示例代碼
這篇文章主要介紹了Nuxt.js實(shí)現(xiàn)一個(gè)SSR的前端博客的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09vue項(xiàng)目中vue-i18n和element-ui國(guó)際化開發(fā)實(shí)現(xiàn)過程
這篇文章主要介紹了vue項(xiàng)目中vue-i18n和element-ui國(guó)際化開發(fā)實(shí)現(xiàn)過程,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-04-04vue中實(shí)現(xiàn)多頁面應(yīng)用方式
這篇文章主要介紹了vue中實(shí)現(xiàn)多頁面應(yīng)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08基于vue和react的spa進(jìn)行按需加載的實(shí)現(xiàn)方法
這篇文章主要介紹了基于vue和react的spa進(jìn)行按需加載,需要的朋友可以參考下2018-09-09