slot内容分发的使用

2023-06-15,,

一、定义了一个组件custom,该组件本身已经具备template模板了,直接调<custom></custom>即可渲染模板

<div id="app">
    <custom></custom>
</div>
<script>
    Vue.component('custom',{
        template:`
            <div class="customStyle">
                <p>custom组件内容一</p>
                <p>custom组件内容二</p>
                <p>custom组件内容三</p>
            </div>
        `
    })
    new Vue({
        el:'#app'
    })
</script>

二、匿名插槽
现在,在使用组件custom的同时,想替换这个组件默认已经定义好的模板,就可以使用slot内容分发
用法:
在<custom></custom>内部写入一些希望展示的html模板,比如
<custom><div>我是自定义的模板</div></custom>,然后,将组件template用<slot></slot>包裹起来,即:

<div id="app">
    <custom><div>我是自定义的模板</div></custom>
</div>
<script>
    Vue.component('custom',{
        template:`
            <div class="customStyle">
                <slot>
                    <p>custom组件内容一</p>
                    <p>custom组件内容二</p>
                    <p>custom组件内容三</p>
                </slot>
            </div>
        `
    })
    new Vue({
        el:'#app'
    })
</script>

那么,
当在custom标签内有自定义的模板时,那么就会替代slot内部的模板内容,渲染到页面
而当在custom标签内没有自定义的模板,那么就会渲染slot内部的模板内容

这就是匿名插槽,不用设置名称属性name,单个插槽可以放置在组件的任意位置,但是就像它的名字一样,一个组件中只能有一个该类插槽。相对应的,具名插槽就可以有很多个,只要名字(名称属性)不同就可以了。

二、具名插槽
在custom标签内有自定义的模板,数量很多,想让custom标签内某部分的模板渲染到,组件内部对应的位置时,就使用具名插槽了

<div id="app">
    <custom>

        <div slot="one">替换组价内容一</div>
        <div slot="three">替换组价内容三</div>

        <template slot="two">  //当自定义的模板内容很多时,就可以使用template括起来,写上slot
            <div>替换组价内容二</div>
            <div>替换组价内容二</div>
            <div>替换组价内容二</div>
            <div>替换组价内容二</div>
            <div>替换组价内容二</div>
        </template>

        <div>替换无名的slot</div>  //没写slot属性值时,就默认替换slot没有name值的那个模板内容

    </custom>
</div>
<script>
    Vue.component('custom',{
        template:`
            <div class="customStyle">

                    <slot name="one><p">custom组件内容一</p></slot>
                    <slot name="two"><p>custom组件内容二</p></slot>
                    <slot name="three"><p>custom组件内容三</p></slot>

                    <slot><p>我是无名的slot</p></slot>

            </div>
        `
    })
    new Vue({
        el:'#app'
    })
</script>

三、编译作用域

<div id="app"> //id为app所在的区域都属于父组件
<custom> //这是父组件,所以这个message渲染的是父组件里的message
{{message}}
</custom>
</div>
<script>
Vue.component('custom',{
data(){
return {
message:'我是子组件的数据'
}
},
template:`
<div class="customStyle">
{{message}} //这是子组件,所以这个message渲染的是子组件里的message
<h3>我是默认的永远都显示的模板内容</h3>
<slot></slot>

        </div>
    `
})
new Vue({
    el:'#app',
    data:{
        message:'我是父组件的数据'
    }
})

</script>



四、总结:匿名插槽:看到自定义组件内容有模板时,直接联想到可以替换组件定义时template的<slot></slot>里的内容,如果模板内容没有slot包裹,则默认全部永远都显示(只要调用了这个组件)
具名插槽:看到自定义组件内容内的模板有slot属性值,则和组件定义时template的<slot></slot>上的name值一一对应

《slot内容分发的使用.doc》

下载本文的Word格式文档,以方便收藏与打印。