slot内容分发的使用-创新互联

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

宁县网站建设公司成都创新互联,宁县网站设计制作,有大型网站制作公司丰富经验。已为宁县上1000+提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的宁县做网站的公司定做!
<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值一一对应

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

文章题目:slot内容分发的使用-创新互联
本文链接:https://www.cdcxhl.com/article24/dscjce.html

成都网站建设公司_创新互联,为您提供网站设计公司App开发网站策划网站制作移动网站建设网站营销

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站建设