vue學(xué)習(xí)筆記之slot插槽用法實例分析
本文實例講述了vue slot插槽用法。分享給大家供大家參考,具體如下:
不使用插槽,在template中用v-html解析父組件傳來的帶有標(biāo)簽的content
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <script src='https://cdn.jsdelivr.net/npm/vue/dist/vue.js'></script></head><body><div id='app'> <child content='<p>Rachel</p>'></child></div></body></html><script> Vue.component(’child’, { props: [’content’], template: ’<div> <p>hello</p> <div v-html='this.content'></div> </div>’ }) var vm = new Vue({ el: ’#app’ })</script>
使用插槽,如果父組件為空,就會顯示slot中定義的默認(rèn)內(nèi)容
<child> <p>Rachel</p></child> Vue.component(’child’, { template: ’<div> <p>hello</p> <slot>默認(rèn)內(nèi)容</slot></div>’})
使用插槽添加header和footer,使用‘具名插槽’,也就是給插槽起個名字,各找各的位置。此處也可以寫默認(rèn)值,如果父組件沒有對應(yīng)的插槽內(nèi)容的話,會顯示子組件定義的插槽的默認(rèn)值。
<div id='app'> <body-content> <div slot='header'>header</div> <div slot='footer'>footer</div> </body-content></div> Vue.component(’body-content’, { template: ’<div> <slot name='header'>default header</slot> <div class='content'>content</div> <slot name='footer'>default footer</slot></div>’})
感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具:http://tools.jb51.net/code/HtmlJsRun測試上述代碼運行效果。
希望本文所述對大家vue.js程序設(shè)計有所幫助。
相關(guān)文章:
1. python中scrapy處理項目數(shù)據(jù)的實例分析2. GIT相關(guān)-IDEA/ECLIPSE工具配置的教程詳解3. js抽獎轉(zhuǎn)盤實現(xiàn)方法分析4. IntelliJ IDEA導(dǎo)入jar包的方法5. 快速搭建Spring Boot+MyBatis的項目IDEA(附源碼下載)6. 教你在 IntelliJ IDEA 中使用 VIM插件的詳細(xì)教程7. Python requests庫參數(shù)提交的注意事項總結(jié)8. 如何基于Python實現(xiàn)word文檔重新排版9. 深入分析PHP設(shè)計模式10. PHP橋接模式Bridge Pattern的優(yōu)點與實現(xiàn)過程
