vue Render中slots的使用

render 中 slot 的一般默认使用方式如下:

this.$slots.default 对用 template的<slot>的使用没有name 。

想使用多个slot 的话。需要对slot命名唯一。使用this.$slots.name 在render中添加多个slot。

<body>
    <div class="" id="app">
    <myslot>
        <div>this is slot</div>
    </myslot>


    </div>
    <script>
    Vue.component('myslot',{
        render:function(createElement){
             var he=createElement('div',{domProps:{innerHTML:'this child div'}});
            return createElement('div',[he,this.$slots.default])
            }
    });
    var app=new Vue({
        el:'#app'
    })
    </script>
    </body> 

多个slot的使用

<body>
    <div class="" id="app">
    <myslot>
        <div slot="name1">this is slot</div>
        <div slot="name2">The position is slot2 </div>
    </myslot>


    </div>
    <script>
    Vue.component('myslot',{
        render:function(createElement){
             var he=createElement('div',{domProps:{innerHTML:'this child div'}});
            return createElement('div',[he,this.$slots.name2,this.$slots.name1])
            }
    });
    var app=new Vue({
        el:'#app'
    })
    </script>
    </body>

vue2.1.0新添加了scope(虽然感觉有点怪,但是用习惯了,还蛮好用的)

同样给出一般使用和多个使用示例,

<body>
    <div class="" id="app">
    <myslot>
        <template scope="props">
            <div>{{props.text}}</div>
        </template>

    </myslot>


    </div>
    <script>
    Vue.component('myslot',{
        render:function(createElement){
             var he=createElement('div',{domProps:{innerHTML:'this child div'}});
            return createElement('div',[he,this.$scopedSlots.default({
                text:'hello scope'
            })])
            }
    });
    var app=new Vue({
        el:'#app'
    })
    </script>
    </body>


多个$scopedSlot的使用

<body>
    <div class="" id="app">
    <myslot>
        <template slot="name2" scope="props">
            <div>{{props.text}}</div>
        </template>
        <template slot="name1" scope="props">
            <span>{{props.text}}</span>
        </template>

    </myslot>


    </div>
    <script>
    Vue.component('myslot',{
        render:function(createElement){
             var he=createElement('div',{domProps:{innerHTML:'this child div'}});
            return createElement('div',
                [he,
                this.$scopedSlots.name1({
                text:'hello scope'
            }),
                this.$scopedSlots.name2({
                text:'$scopedSlots using'
            })])
            }
    });
    var app=new Vue({
        el:'#app'
    })
    </script>
    </body>


  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值