slot内容分发的使用

发布时间:2020-06-20 13:03:34 作者:xxxpjgl
来源:网络 阅读:467

一、定义了一个组件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,单个插槽可以放置在组件的任意位置,但是就像它的名字一样,一个组件中只能有一个该类插槽。相对应的,具名插槽就可以有很多个,只要名字(名称属性)不同就可以了。
slot内容分发的使用

二、具名插槽
在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>

slot内容分发的使用

三、编译作用域
slot内容分发的使用
<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值一一对应
推荐阅读:
  1. 如何vue中使用slot分发内容
  2. Vue中内容分发<slot>​的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

slot 使用

上一篇:700K+ IOPS 响应时间<0.5ms 新全闪阵列

下一篇:vCenter5.1安装详解

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》