vue Render中slots的使用的实例代码
本文介绍了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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
el-checkbox-group 的v-model无法绑定对象数组的问题解决
elementUI官方文档中el-checkbox-group组件绑定的都为一维数组,本文主要介绍了解决el-checkbox-group 的v-model无法绑定对象数组,感兴趣的可以了解一下2023-05-05
解决获取数据后this.$refs.xxx.toggleRowSelection无效的问题
这篇文章主要介绍了解决获取数据后this.$refs.xxx.toggleRowSelection无效的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-10-10
vue3中路由传参query、params及动态路由传参详解
vue3中的传参方式和vue2中一样,都可以用query和params传参,下面这篇文章主要给大家介绍了关于vue3中路由传参query、params及动态路由传参的相关资料,需要的朋友可以参考下2022-09-09
Vue中对比scoped css和css module的区别
这篇文章主要介绍了Vue中scoped css和css module的区别对比,scoped css可以直接在能跑起来的vue项目中使用而css module需要增加css-loader配置才能生效。具体内容详情大家参考下本文2018-05-05


最新评论