Vue學(xué)習(xí)日記---插槽

在開發(fā)項(xiàng)目的過程中插槽的使用是不可避免地,而且在View、Element-ui這些框架中都會(huì)有插槽的影子,今天就來(lái)說(shuō)說(shuō)插槽的使用。

<div id="app">
    <navigation-link url="/profile">
        Your Profile
    </navigation-link>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    Vue.component('navigation-link',{
        props: {
          url: String
        },
        template: `<a v-bind:href="url"
                        class="nav-link">
                        <slot></slot>
                    </a>`
    });
    new Vue({
        el: '#app'
    })
</script>

這也就是插槽最簡(jiǎn)單的使用方法啦,插槽內(nèi)可以包含任何模板代碼,包括組件。

image.png

這里引用vue官網(wǎng)上的一句話,也就是說(shuō)如果上述代碼中的navigation-link這個(gè)組件中不包含<slot>元素,則組件標(biāo)簽中的內(nèi)容Your Profile將不會(huì)被渲染。

具名插槽
<div id="app">
    <navigation-link v-slot="header" url="/profile">
        Your Profile
    </navigation-link>
    <base-layout>
        <template v-slot:header>
            <h2>我是導(dǎo)航</h2>
        </template>
        <template v-slot:default>
            <h2>我是內(nèi)容</h2>
        </template>
        <template v-slot:footer>
            <h2>我是底部</h2>
        </template>
    </base-layout>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    Vue.component('navigation-link',{
        props: {
          url: String
        },
        template: `<a v-bind:href="url"
                        class="nav-link">
                        <slot></slot>
                    </a>`
    });
    const baseLayout = {
        template: `<div class="container">
                        <header><slot name="header"></slot></header>
                        <main><slot></slot></main>
                        <footer><slot name="footer"></slot></footer>
                    </div>`
    };
    new Vue({
        el: '#app',
        components: {
            'base-layout': baseLayout
        }
    })
</script>

跟著文檔走一遍,具名插槽只會(huì)渲染通過v-slot綁定的組件,沒有定義name的插槽默認(rèn)為default

作用域插槽

還是引入官方的例子

<div id="app">
    <current-user>
        <template v-slot:default="slotProps">
            {{slotProps.user.firstName}}
        </template>
    </current-user>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
    const currentUser = {
        data() {
          return {
              user: {
                  firstName: 'chen',
                  lastName: 'jx'
              }
          }
        },
        template: `<div>
                        <span>
                              <slot v-bind:user="user">{{user.lastName}}</slot>
                        </span>
                    </div>`
    }
    new Vue({
        el: '#app',
        components: {
            'base-layout': baseLayout,
            'current-user': currentUser
        }
    })
</script>

插槽的主要用法就是這些,還有一些就是插槽的縮寫語(yǔ)法,這些官方都解釋的很清楚,就不在舉例子啦!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 【2019-3-4更新】Vue 2.6+修改了部分語(yǔ)法,對(duì)插槽的使用有了較多的更新。在本文中筆者在相應(yīng)位置給出了更...
    果汁涼茶丶閱讀 10,502評(píng)論 2 36
  • 什么是組件? 組件 (Component) 是 Vue.js 最強(qiáng)大的功能之一。組件可以擴(kuò)展 HTML 元素,封裝...
    youins閱讀 9,705評(píng)論 0 13
  • 探索Vue高階組件 高階組件(HOC)是 React 生態(tài)系統(tǒng)的常用詞匯,React 中代碼復(fù)用的主要方式就是使用...
    君惜丶閱讀 1,057評(píng)論 0 2
  • 深入理解vue中的slot與slot-scope 插槽,也就是slot,是組件的一塊HTML模板。 對(duì)于任何一個(gè)組...
    趙客縵胡纓v吳鉤霜雪明閱讀 313評(píng)論 0 5
  • 深入理解vue中的slot與slot-scope 寫在前面 vue中關(guān)于插槽的文檔說(shuō)明很短,語(yǔ)言又寫的很凝練,再加...
    SentMes閱讀 42,839評(píng)論 14 55

友情鏈接更多精彩內(nèi)容