欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

vue Render中slots的使用的实例代码

程序员文章站 2023-02-24 08:26:38
本文介绍了vue render中slots的使用的实例代码,有需要了解vue render中slots用法的朋友可参考。希望此文章对各位有所帮助。 render 中...

本文介绍了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> 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。