這期內(nèi)容當中小編將會給大家?guī)碛嘘P(guān)slot插槽怎么在vue中使用,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

不使用插槽,在template中用v-html解析父組件傳來的帶有標簽的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中定義的默認內(nèi)容
<child>
<p>Rachel</p>
</child>
Vue.component('child', {
template: '<div>
<p>hello</p>
<slot>默認內(nèi)容</slot>
</div>'
})使用插槽添加header和footer,使用‘具名插槽',也就是給插槽起個名字,各找各的位置。此處也可以寫默認值,如果父組件沒有對應(yīng)的插槽內(nèi)容的話,會顯示子組件定義的插槽的默認值。
<div id="app">
<body-content>
<div class="header" slot="header">header</div>
<div class="footer" 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>'
})Vue的優(yōu)點Vue具體輕量級框架、簡單易學、雙向數(shù)據(jù)綁定、組件化、數(shù)據(jù)和結(jié)構(gòu)的分離、虛擬DOM、運行速度快等優(yōu)勢,Vue中頁面使用的是局部刷新,不用每次跳轉(zhuǎn)頁面都要請求所有數(shù)據(jù)和dom,可以大大提升訪問速度和用戶體驗。
上述就是小編為大家分享的slot插槽怎么在vue中使用了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享標題:slot插槽怎么在vue中使用-創(chuàng)新互聯(lián)
分享地址:http://chinadenli.net/article32/dppjsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、品牌網(wǎng)站設(shè)計、網(wǎng)站內(nèi)鏈、微信小程序、面包屑導航、網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容