春天来了,花园里开满了花,有梅花,有迎春花,有山茶花,有丁香花,真是太好看了。
本文介绍了vue Render中slots的使用的实例代码,有需要了解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>
本文vue Render中slots的如何使用的实例代码到此结束。别人可以替你开车,但不能替你走路;可以替你做事,但不能替你感受。人生的路要靠自己行走,成功要靠自己去争取。天助自助者,成功者自救。小编再次感谢大家对我们的支持!