当前位置:   article > 正文

vue Render中slots的使用_vue render slot

vue render slot

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

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

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

  1. <body>
  2. <div class="" id="app">
  3. <myslot>
  4. <div>this is slot</div>
  5. </myslot>
  6. </div>
  7. <script>
  8. Vue.component('myslot',{
  9. render:function(createElement){
  10. var he=createElement('div',{domProps:{innerHTML:'this child div'}});
  11. return createElement('div',[he,this.$slots.default])
  12. }
  13. });
  14. var app=new Vue({
  15. el:'#app'
  16. })
  17. </script>
  18. </body>

多个slot的使用

  1. <body>
  2. <div class="" id="app">
  3. <myslot>
  4. <div slot="name1">this is slot</div>
  5. <div slot="name2">The position is slot2 </div>
  6. </myslot>
  7. </div>
  8. <script>
  9. Vue.component('myslot',{
  10. render:function(createElement){
  11. var he=createElement('div',{domProps:{innerHTML:'this child div'}});
  12. return createElement('div',[he,this.$slots.name2,this.$slots.name1])
  13. }
  14. });
  15. var app=new Vue({
  16. el:'#app'
  17. })
  18. </script>
  19. </body>

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

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

  1. <body>
  2. <div class="" id="app">
  3. <myslot>
  4. <template scope="props">
  5. <div>{{props.text}}</div>
  6. </template>
  7. </myslot>
  8. </div>
  9. <script>
  10. Vue.component('myslot',{
  11. render:function(createElement){
  12. var he=createElement('div',{domProps:{innerHTML:'this child div'}});
  13. return createElement('div',[he,this.$scopedSlots.default({
  14. text:'hello scope'
  15. })])
  16. }
  17. });
  18. var app=new Vue({
  19. el:'#app'
  20. })
  21. </script>
  22. </body>


多个$scopedSlot的使用

  1. <body>
  2. <div class="" id="app">
  3. <myslot>
  4. <template slot="name2" scope="props">
  5. <div>{{props.text}}</div>
  6. </template>
  7. <template slot="name1" scope="props">
  8. <span>{{props.text}}</span>
  9. </template>
  10. </myslot>
  11. </div>
  12. <script>
  13. Vue.component('myslot',{
  14. render:function(createElement){
  15. var he=createElement('div',{domProps:{innerHTML:'this child div'}});
  16. return createElement('div',
  17. [he,
  18. this.$scopedSlots.name1({
  19. text:'hello scope'
  20. }),
  21. this.$scopedSlots.name2({
  22. text:'$scopedSlots using'
  23. })])
  24. }
  25. });
  26. var app=new Vue({
  27. el:'#app'
  28. })
  29. </script>
  30. </body>


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/991026
推荐阅读
相关标签
  

闽ICP备14008679号