ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Vue:slot用法

2020-03-04 12:01:13  阅读:324  来源: 互联网

标签:slot Vue szonline 用法 组件 net htmlhttp market amaz


单个 Slot
在子组件内使用特殊的<slot>元素就可以为这个子组件添加一个 slot (插槽),在父组件模板里,插入在子组件标签内的所有内容将替代子组件的<slot>标签及它的内容.示例代码如下:


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>示例</title>

</head>
<body>

    <div id="app">
        <child-component>
            <p>分发的内容</p>
            <p>更多分发的内容</p>
        </child-component>
    </div>
    
    <script src="https://unpkg.com/vue/dist/vue.min.js"></script>

    <script>

        Vue.component('child-component', {
            template: '\
            <div>\
                <slot>\
                    <p>如果父组件没用插入内容,我将作为默认出现</p>\
                </slot>\
            </div>'
        });

        var app = new Vue({
            el: '#app'
        })

    </script>

</body>
</html>


子组件 child-component 的模板内定义一个 <slot> 元素,并且用一个 <p> 作为默认的内容,在父组件没有使用 slot 时,会渲染这段默认的文本;如果写入了 slot ,那就会替换整个 <slot>.所以上列渲染后的结果为:

<div id="app">
     <div>
        <p>分发的内容</p>
        <p>更多分发的内容</p>
    </div>
</div>

注意:子组件<slot>内的备用内容,它的作用域时子组件本身.
具名 Slot
给 <slot> 元素指定一个 name 后可以分发多个内容,具名 Slot 可以与单个 Slot 共存,例如下面的示例:


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>示例</title>

</head>
<body>

    <div id="app">
        <child-component>
            <h2 slot="header">标题</h2>
            <p>正文内容</p>
            <p>更多正文内容</p>
            <div slot="footer">底部信息</div>
        </child-component>
    </div>
    
    <script src="https://unpkg.com/vue/dist/vue.min.js"></script>

    <script>

        Vue.component('child-component', {
            template: '\
            <div class="component">\
                <div class="header">\
                    <slot name="header"></slot>\
                </div>\
                <div class="main">\
                    <slot></slot>\
                </div>\
                <div class="footer">\
                    <slot name="footer"></slot>\
                </div>\
            </div>'
        });

        var app = new Vue({
            el: '#app'
        })

    </script>

</body>
</html>


子组件内声明了3个 <slot> 元素,其中在<div class="main">内的<slot> 没用使用 name 特性,它将作为默认 slot 出现,父组件没有使用 slot 特性的元素与内容都将出现在这里.
如果没有指定默认的匿名 slot, 父组件内多余的内容片段都将被抛弃.
上例最终渲染后的结果为:

<div id="app">
        <div class="container">
            <div class="header">
                <h2>标题</h2>
            </div>
            <div class="main">
                <p>正文内容</p>
                <p>更多的正文内容</p>
            </div>
            <div class="footer">
                <div>底部信息</div>
            </div>
        </div>
    </div>

在组合使用组件时,内容分发API至关重要.



作者:一叶扁舟丶
链接:https://www.jianshu.com/p/559332a9c123
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

http://market.szonline.net/amaz/33831.html
http://market.szonline.net/amaz/33829.html
http://market.szonline.net/amaz/33827.html
http://market.szonline.net/amaz/33823.html
http://market.szonline.net/amaz/33818.html
http://market.szonline.net/amaz/33812.html
http://market.szonline.net/amaz/33807.html
http://market.szonline.net/amaz/33801.html
http://market.szonline.net/amaz/33796.html
http://market.szonline.net/amaz/33791.html
http://market.szonline.net/amaz/33786.html
http://market.szonline.net/amaz/33785.html
http://market.szonline.net/amaz/33782.html
http://market.szonline.net/amaz/33777.html
http://market.szonline.net/amaz/33771.html
http://market.szonline.net/amaz/33769.html
http://market.szonline.net/amaz/33767.html
http://market.szonline.net/amaz/33764.html
http://market.szonline.net/amaz/33763.html
http://market.szonline.net/amaz/33762.html
http://market.szonline.net/amaz/33761.html
http://market.szonline.net/amaz/33757.html
http://market.szonline.net/amaz/33751.html
http://market.szonline.net/amaz/33745.html
http://market.szonline.net/amaz/33740.html
http://market.szonline.net/amaz/33733.html
http://market.szonline.net/amaz/33728.html

标签:slot,Vue,szonline,用法,组件,net,htmlhttp,market,amaz
来源: https://www.cnblogs.com/zjw2004112/p/12408639.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有